feat:增加消息功能

This commit is contained in:
2026-06-18 14:20:37 +08:00
parent b816cdbc1c
commit bb1160ad18
7 changed files with 76 additions and 53 deletions

View File

@@ -1,5 +1,3 @@
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';
@@ -15,11 +13,13 @@ class ChatMessage {
}
class ChatPage extends StatefulWidget {
final int contractId;
final String contactName;
final String avatarUrl;
const ChatPage({
super.key,
required this.contractId,
required this.contactName,
required this.avatarUrl,
});
@@ -32,48 +32,46 @@ class _ChatPageState extends State<ChatPage> {
final TextEditingController _controller = TextEditingController();
final ScrollController _scrollController = ScrollController();
late int userId;
late List<ChatMessage> _messages;
@override
void initState() {
super.initState();
_messages = [
// ChatMessage(text: '在吗?', isMe: false, avatar: widget.avatarUrl),
// ChatMessage(text: '在的,怎么啦 😊', isMe: true),
// ChatMessage(text: '今晚一起吃饭吗?', isMe: false, avatar: widget.avatarUrl),
];
_messages = [];
final userId = SpUtil.getInt('userId') ?? 0;
userId = SpUtil.getInt('userId') ?? 0;
final online = ImMessage(
type: "ONLINE",
from: userId.toString(),
to: '',
to: 'SYSTEM',
content: '',
);
WebSocketService().connect('ws://192.168.31.108:5050?userId=$userId');
WebSocketService().send(jsonEncode(online.toJson()));
WebSocketService().messages.listen((msg) {
print('收到消息: $msg');
WebSocketService().connect('ws://172.29.100.146:5050?userId=$userId');
WebSocketService().send(online.toJsonString());
WebSocketService().messages.listen((wsMsg) {
final ImMessage imMsg = ImMessage.fromJsonString(wsMsg);
setState(() {
_messages.add(ChatMessage(text: msg, isMe: false, avatar: widget.avatarUrl));
if (imMsg.type.contains('CHAT')) {
_messages.add(
ChatMessage(
text: imMsg.content,
isMe: userId.toString() == imMsg.from,
avatar: widget.avatarUrl,
),
);
}
});
_controller.clear();
Future.delayed(const Duration(milliseconds: 100), () {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
});
_controller.clear();
scrollToBottom();
});
}
@override
void dispose() {
super.dispose();
@@ -84,19 +82,13 @@ class _ChatPageState extends State<ChatPage> {
final text = _controller.text.trim();
if (text.isEmpty) return;
setState(() {
_messages.add(ChatMessage(text: text, isMe: true));
});
_controller.clear();
Future.delayed(const Duration(milliseconds: 100), () {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
});
final chat = ImMessage(
type: "CHAT_PRIVATE",
from: userId.toString(),
to: widget.contractId.toString(),
content: text,
);
WebSocketService().send(chat.toJsonString());
}
Widget _buildAvatar(String? url) {
@@ -167,7 +159,11 @@ class _ChatPageState extends State<ChatPage> {
children: [
if (!msg.isMe) _buildAvatar(msg.avatar),
Flexible(
child: GlassCard(
child: Container(
decoration: BoxDecoration(
color: msg.isMe ? Color(0xFF2FA951): Color(0xFF030816),
borderRadius: BorderRadius.circular(12),
),
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 10,
@@ -199,6 +195,7 @@ class _ChatPageState extends State<ChatPage> {
controller: _controller,
onSubmitted: (_) => _sendMessage(),
style: const TextStyle(color: Colors.white),
cursorColor: Colors.white,
decoration: const InputDecoration(
hintText: '输入消息...',
hintStyle: TextStyle(color: Colors.white),
@@ -215,4 +212,14 @@ class _ChatPageState extends State<ChatPage> {
),
);
}
void scrollToBottom() {
Future.delayed(const Duration(milliseconds: 100), () {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
});
}
}