feat:增加消息功能
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class ImMessage {
|
||||
final String type;
|
||||
final String from;
|
||||
@@ -11,10 +13,27 @@ class ImMessage {
|
||||
required this.content,
|
||||
});
|
||||
|
||||
factory ImMessage.fromJsonString(String json) {
|
||||
return ImMessage.fromJson(jsonDecode(json));
|
||||
}
|
||||
|
||||
factory ImMessage.fromJson(Map<String, dynamic> json) {
|
||||
return ImMessage(
|
||||
type: json['type'],
|
||||
from: json['from'],
|
||||
to: json['to'],
|
||||
content: json['content'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'type': type,
|
||||
'from': from,
|
||||
'to': to,
|
||||
'content': content,
|
||||
};
|
||||
|
||||
String toJsonString() {
|
||||
return jsonEncode(toJson());
|
||||
}
|
||||
}
|
||||
@@ -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,45 +32,43 @@ 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().connect('ws://172.29.100.146:5050?userId=$userId');
|
||||
WebSocketService().send(online.toJsonString());
|
||||
|
||||
WebSocketService().messages.listen((msg) {
|
||||
print('收到消息: $msg');
|
||||
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,
|
||||
);
|
||||
});
|
||||
scrollToBottom();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,12 +8,14 @@ import 'package:sweet_chat_app/utils/SpUtil.dart';
|
||||
import 'chat_page.dart';
|
||||
|
||||
class ContactInfo extends ISuspensionBean {
|
||||
final int id;
|
||||
final String name;
|
||||
final String avatar;
|
||||
String? tagIndex;
|
||||
String? namePinyin;
|
||||
|
||||
ContactInfo({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.avatar,
|
||||
this.tagIndex,
|
||||
@@ -58,6 +60,7 @@ class _ContactsPageState extends State<ContactsPage> {
|
||||
final tag = pinyin.substring(0, 1).toUpperCase();
|
||||
|
||||
return ContactInfo(
|
||||
id: user.id,
|
||||
name: user.nickname,
|
||||
avatar: user.avatarUrl,
|
||||
tagIndex: RegExp(r'^[A-Z]$').hasMatch(tag) ? tag : '#',
|
||||
@@ -163,6 +166,7 @@ class _ContactsPageState extends State<ContactsPage> {
|
||||
MaterialPageRoute(
|
||||
builder:
|
||||
(_) => ChatPage(
|
||||
contractId: contact.id,
|
||||
contactName: contact.name,
|
||||
avatarUrl: contact.avatar,
|
||||
),
|
||||
|
||||
@@ -3,9 +3,6 @@ import 'package:fluttertoast/fluttertoast.dart';
|
||||
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
|
||||
import 'package:sweet_chat_app/apis/auth_api.dart';
|
||||
import 'package:sweet_chat_app/utils/SpUtil.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
import '../services/webSocket_service.dart';
|
||||
import 'home_page.dart';
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
@@ -71,10 +68,11 @@ class _LoginPageState extends State<LoginPage> {
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 邮箱
|
||||
// 用户名
|
||||
TextField(
|
||||
controller: _usernameController,
|
||||
style: const TextStyle(color: Colors.white),
|
||||
cursorColor: Colors.white,
|
||||
decoration: InputDecoration(
|
||||
hintText: '请输入用户名',
|
||||
hintStyle: TextStyle(color: Colors.white70),
|
||||
@@ -94,6 +92,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||
controller: _passwordController,
|
||||
obscureText: true,
|
||||
style: const TextStyle(color: Colors.white),
|
||||
cursorColor: Colors.white,
|
||||
decoration: InputDecoration(
|
||||
hintText: '请输入密码',
|
||||
hintStyle: TextStyle(color: Colors.white70),
|
||||
|
||||
@@ -79,6 +79,7 @@ class MessagePage extends StatelessWidget {
|
||||
MaterialPageRoute(
|
||||
builder:
|
||||
(_) => const ChatPage(
|
||||
contractId: 0,
|
||||
contactName: '张三',
|
||||
avatarUrl: 'https://picsum.photos/id/1012/200',
|
||||
),
|
||||
@@ -125,11 +126,4 @@ class Message {
|
||||
});
|
||||
}
|
||||
|
||||
List<Message> _messages = [
|
||||
Message(
|
||||
avatar: 'https://picsum.photos/id/1012/200',
|
||||
name: '小刘',
|
||||
lastMsg: '今晚一起吃饭吗?',
|
||||
time: '12:30',
|
||||
),
|
||||
];
|
||||
List<Message> _messages = [];
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
final dioService = Dio(BaseOptions(
|
||||
baseUrl: 'http://192.168.31.108:2836/api',
|
||||
baseUrl: 'http://172.29.100.146:2836/api',
|
||||
connectTimeout: Duration(seconds: 10),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
));
|
||||
Reference in New Issue
Block a user