feat:增加服务器接口模块
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
|
||||
import 'package:sweet_chat_app/models/ImMessage.dart';
|
||||
import 'package:sweet_chat_app/services/webSocket_service.dart';
|
||||
import 'package:sweet_chat_app/utils/SpUtil.dart';
|
||||
|
||||
class ChatMessage {
|
||||
final String text;
|
||||
@@ -34,10 +39,45 @@ class _ChatPageState extends State<ChatPage> {
|
||||
super.initState();
|
||||
|
||||
_messages = [
|
||||
ChatMessage(text: '在吗?', isMe: false, avatar: widget.avatarUrl),
|
||||
ChatMessage(text: '在的,怎么啦 😊', isMe: true),
|
||||
ChatMessage(text: '今晚一起吃饭吗?', isMe: false, avatar: widget.avatarUrl),
|
||||
// ChatMessage(text: '在吗?', isMe: false, avatar: widget.avatarUrl),
|
||||
// ChatMessage(text: '在的,怎么啦 😊', isMe: true),
|
||||
// ChatMessage(text: '今晚一起吃饭吗?', isMe: false, avatar: widget.avatarUrl),
|
||||
];
|
||||
|
||||
final userId = SpUtil.getInt('userId') ?? 0;
|
||||
final online = ImMessage(
|
||||
type: "ONLINE",
|
||||
from: userId.toString(),
|
||||
to: '',
|
||||
content: '',
|
||||
);
|
||||
|
||||
WebSocketService().connect('ws://192.168.31.108:5050?userId=$userId');
|
||||
WebSocketService().send(jsonEncode(online.toJson()));
|
||||
|
||||
WebSocketService().messages.listen((msg) {
|
||||
print('收到消息: $msg');
|
||||
|
||||
setState(() {
|
||||
_messages.add(ChatMessage(text: msg, isMe: false, avatar: widget.avatarUrl));
|
||||
});
|
||||
|
||||
_controller.clear();
|
||||
|
||||
Future.delayed(const Duration(milliseconds: 100), () {
|
||||
_scrollController.animateTo(
|
||||
_scrollController.position.maxScrollExtent,
|
||||
duration: const Duration(milliseconds: 300),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
WebSocketService().disconnect();
|
||||
}
|
||||
|
||||
void _sendMessage() {
|
||||
|
||||
Reference in New Issue
Block a user