feat:增加消息功能
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
class ImMessage {
|
class ImMessage {
|
||||||
final String type;
|
final String type;
|
||||||
final String from;
|
final String from;
|
||||||
@@ -11,10 +13,27 @@ class ImMessage {
|
|||||||
required this.content,
|
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() => {
|
Map<String, dynamic> toJson() => {
|
||||||
'type': type,
|
'type': type,
|
||||||
'from': from,
|
'from': from,
|
||||||
'to': to,
|
'to': to,
|
||||||
'content': content,
|
'content': content,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
String toJsonString() {
|
||||||
|
return jsonEncode(toJson());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
|
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
|
||||||
import 'package:sweet_chat_app/models/ImMessage.dart';
|
import 'package:sweet_chat_app/models/ImMessage.dart';
|
||||||
@@ -15,11 +13,13 @@ class ChatMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ChatPage extends StatefulWidget {
|
class ChatPage extends StatefulWidget {
|
||||||
|
final int contractId;
|
||||||
final String contactName;
|
final String contactName;
|
||||||
final String avatarUrl;
|
final String avatarUrl;
|
||||||
|
|
||||||
const ChatPage({
|
const ChatPage({
|
||||||
super.key,
|
super.key,
|
||||||
|
required this.contractId,
|
||||||
required this.contactName,
|
required this.contactName,
|
||||||
required this.avatarUrl,
|
required this.avatarUrl,
|
||||||
});
|
});
|
||||||
@@ -32,45 +32,43 @@ class _ChatPageState extends State<ChatPage> {
|
|||||||
final TextEditingController _controller = TextEditingController();
|
final TextEditingController _controller = TextEditingController();
|
||||||
final ScrollController _scrollController = ScrollController();
|
final ScrollController _scrollController = ScrollController();
|
||||||
|
|
||||||
|
late int userId;
|
||||||
late List<ChatMessage> _messages;
|
late List<ChatMessage> _messages;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
_messages = [
|
_messages = [];
|
||||||
// ChatMessage(text: '在吗?', isMe: false, avatar: widget.avatarUrl),
|
|
||||||
// ChatMessage(text: '在的,怎么啦 😊', isMe: true),
|
|
||||||
// ChatMessage(text: '今晚一起吃饭吗?', isMe: false, avatar: widget.avatarUrl),
|
|
||||||
];
|
|
||||||
|
|
||||||
final userId = SpUtil.getInt('userId') ?? 0;
|
userId = SpUtil.getInt('userId') ?? 0;
|
||||||
final online = ImMessage(
|
final online = ImMessage(
|
||||||
type: "ONLINE",
|
type: "ONLINE",
|
||||||
from: userId.toString(),
|
from: userId.toString(),
|
||||||
to: '',
|
to: 'SYSTEM',
|
||||||
content: '',
|
content: '',
|
||||||
);
|
);
|
||||||
|
|
||||||
WebSocketService().connect('ws://192.168.31.108:5050?userId=$userId');
|
WebSocketService().connect('ws://172.29.100.146:5050?userId=$userId');
|
||||||
WebSocketService().send(jsonEncode(online.toJson()));
|
WebSocketService().send(online.toJsonString());
|
||||||
|
|
||||||
WebSocketService().messages.listen((msg) {
|
WebSocketService().messages.listen((wsMsg) {
|
||||||
print('收到消息: $msg');
|
final ImMessage imMsg = ImMessage.fromJsonString(wsMsg);
|
||||||
|
|
||||||
setState(() {
|
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();
|
_controller.clear();
|
||||||
|
scrollToBottom();
|
||||||
Future.delayed(const Duration(milliseconds: 100), () {
|
|
||||||
_scrollController.animateTo(
|
|
||||||
_scrollController.position.maxScrollExtent,
|
|
||||||
duration: const Duration(milliseconds: 300),
|
|
||||||
curve: Curves.easeOut,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,19 +82,13 @@ class _ChatPageState extends State<ChatPage> {
|
|||||||
final text = _controller.text.trim();
|
final text = _controller.text.trim();
|
||||||
if (text.isEmpty) return;
|
if (text.isEmpty) return;
|
||||||
|
|
||||||
setState(() {
|
final chat = ImMessage(
|
||||||
_messages.add(ChatMessage(text: text, isMe: true));
|
type: "CHAT_PRIVATE",
|
||||||
});
|
from: userId.toString(),
|
||||||
|
to: widget.contractId.toString(),
|
||||||
_controller.clear();
|
content: text,
|
||||||
|
);
|
||||||
Future.delayed(const Duration(milliseconds: 100), () {
|
WebSocketService().send(chat.toJsonString());
|
||||||
_scrollController.animateTo(
|
|
||||||
_scrollController.position.maxScrollExtent,
|
|
||||||
duration: const Duration(milliseconds: 300),
|
|
||||||
curve: Curves.easeOut,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildAvatar(String? url) {
|
Widget _buildAvatar(String? url) {
|
||||||
@@ -167,7 +159,11 @@ class _ChatPageState extends State<ChatPage> {
|
|||||||
children: [
|
children: [
|
||||||
if (!msg.isMe) _buildAvatar(msg.avatar),
|
if (!msg.isMe) _buildAvatar(msg.avatar),
|
||||||
Flexible(
|
Flexible(
|
||||||
child: GlassCard(
|
child: Container(
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: msg.isMe ? Color(0xFF2FA951): Color(0xFF030816),
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
),
|
||||||
padding: const EdgeInsets.symmetric(
|
padding: const EdgeInsets.symmetric(
|
||||||
horizontal: 12,
|
horizontal: 12,
|
||||||
vertical: 10,
|
vertical: 10,
|
||||||
@@ -199,6 +195,7 @@ class _ChatPageState extends State<ChatPage> {
|
|||||||
controller: _controller,
|
controller: _controller,
|
||||||
onSubmitted: (_) => _sendMessage(),
|
onSubmitted: (_) => _sendMessage(),
|
||||||
style: const TextStyle(color: Colors.white),
|
style: const TextStyle(color: Colors.white),
|
||||||
|
cursorColor: Colors.white,
|
||||||
decoration: const InputDecoration(
|
decoration: const InputDecoration(
|
||||||
hintText: '输入消息...',
|
hintText: '输入消息...',
|
||||||
hintStyle: TextStyle(color: Colors.white),
|
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';
|
import 'chat_page.dart';
|
||||||
|
|
||||||
class ContactInfo extends ISuspensionBean {
|
class ContactInfo extends ISuspensionBean {
|
||||||
|
final int id;
|
||||||
final String name;
|
final String name;
|
||||||
final String avatar;
|
final String avatar;
|
||||||
String? tagIndex;
|
String? tagIndex;
|
||||||
String? namePinyin;
|
String? namePinyin;
|
||||||
|
|
||||||
ContactInfo({
|
ContactInfo({
|
||||||
|
required this.id,
|
||||||
required this.name,
|
required this.name,
|
||||||
required this.avatar,
|
required this.avatar,
|
||||||
this.tagIndex,
|
this.tagIndex,
|
||||||
@@ -58,6 +60,7 @@ class _ContactsPageState extends State<ContactsPage> {
|
|||||||
final tag = pinyin.substring(0, 1).toUpperCase();
|
final tag = pinyin.substring(0, 1).toUpperCase();
|
||||||
|
|
||||||
return ContactInfo(
|
return ContactInfo(
|
||||||
|
id: user.id,
|
||||||
name: user.nickname,
|
name: user.nickname,
|
||||||
avatar: user.avatarUrl,
|
avatar: user.avatarUrl,
|
||||||
tagIndex: RegExp(r'^[A-Z]$').hasMatch(tag) ? tag : '#',
|
tagIndex: RegExp(r'^[A-Z]$').hasMatch(tag) ? tag : '#',
|
||||||
@@ -163,6 +166,7 @@ class _ContactsPageState extends State<ContactsPage> {
|
|||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder:
|
builder:
|
||||||
(_) => ChatPage(
|
(_) => ChatPage(
|
||||||
|
contractId: contact.id,
|
||||||
contactName: contact.name,
|
contactName: contact.name,
|
||||||
avatarUrl: contact.avatar,
|
avatarUrl: contact.avatar,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ import 'package:fluttertoast/fluttertoast.dart';
|
|||||||
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
|
import 'package:liquid_glass_widgets/liquid_glass_widgets.dart';
|
||||||
import 'package:sweet_chat_app/apis/auth_api.dart';
|
import 'package:sweet_chat_app/apis/auth_api.dart';
|
||||||
import 'package:sweet_chat_app/utils/SpUtil.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';
|
import 'home_page.dart';
|
||||||
|
|
||||||
class LoginPage extends StatefulWidget {
|
class LoginPage extends StatefulWidget {
|
||||||
@@ -71,10 +68,11 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
),
|
),
|
||||||
const SizedBox(height: 24),
|
const SizedBox(height: 24),
|
||||||
|
|
||||||
// 邮箱
|
// 用户名
|
||||||
TextField(
|
TextField(
|
||||||
controller: _usernameController,
|
controller: _usernameController,
|
||||||
style: const TextStyle(color: Colors.white),
|
style: const TextStyle(color: Colors.white),
|
||||||
|
cursorColor: Colors.white,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText: '请输入用户名',
|
hintText: '请输入用户名',
|
||||||
hintStyle: TextStyle(color: Colors.white70),
|
hintStyle: TextStyle(color: Colors.white70),
|
||||||
@@ -94,6 +92,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
controller: _passwordController,
|
controller: _passwordController,
|
||||||
obscureText: true,
|
obscureText: true,
|
||||||
style: const TextStyle(color: Colors.white),
|
style: const TextStyle(color: Colors.white),
|
||||||
|
cursorColor: Colors.white,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText: '请输入密码',
|
hintText: '请输入密码',
|
||||||
hintStyle: TextStyle(color: Colors.white70),
|
hintStyle: TextStyle(color: Colors.white70),
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ class MessagePage extends StatelessWidget {
|
|||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder:
|
builder:
|
||||||
(_) => const ChatPage(
|
(_) => const ChatPage(
|
||||||
|
contractId: 0,
|
||||||
contactName: '张三',
|
contactName: '张三',
|
||||||
avatarUrl: 'https://picsum.photos/id/1012/200',
|
avatarUrl: 'https://picsum.photos/id/1012/200',
|
||||||
),
|
),
|
||||||
@@ -125,11 +126,4 @@ class Message {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Message> _messages = [
|
List<Message> _messages = [];
|
||||||
Message(
|
|
||||||
avatar: 'https://picsum.photos/id/1012/200',
|
|
||||||
name: '小刘',
|
|
||||||
lastMsg: '今晚一起吃饭吗?',
|
|
||||||
time: '12:30',
|
|
||||||
),
|
|
||||||
];
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
|
|
||||||
final dioService = Dio(BaseOptions(
|
final dioService = Dio(BaseOptions(
|
||||||
baseUrl: 'http://192.168.31.108:2836/api',
|
baseUrl: 'http://172.29.100.146:2836/api',
|
||||||
connectTimeout: Duration(seconds: 10),
|
connectTimeout: Duration(seconds: 10),
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
));
|
));
|
||||||
@@ -1 +1 @@
|
|||||||
name: sweet_chat_app
|
name: sweet_chat_app
|
||||||
Reference in New Issue
Block a user