Browse Source

feat(message): 实现聊天页面基础功能

- 添加 ChatController 管理聊天逻辑
- 实现用户信息获取与消息发送功能
- 创建 ChatInputBar 组件处理消息输入
- 更新 ChatPage 使用 GetBuilder 构建界面
- 集成 IMManager 处理即时通讯逻辑
- 添加消息列表基础布局与空内容展示
- 实现发送按钮交互与文本输入控制
ios
Jolie 4 months ago
parent
commit
4eb3dd1278
3 changed files with 175 additions and 69 deletions
  1. 54
      lib/controller/message/chat_controller.dart
  2. 90
      lib/pages/message/chat_page.dart
  3. 100
      lib/widget/message/chat_input_bar.dart

54
lib/controller/message/chat_controller.dart

@ -0,0 +1,54 @@
import 'package:get/get.dart';
import '../../im/im_manager.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
class ChatController extends GetxController {
final String userId;
//
final Rx<EMUserInfo?> userInfo = Rx<EMUserInfo?>(null);
ChatController({required this.userId});
@override
void onInit() {
super.onInit();
//
fetchUserInfo();
}
///
Future<void> fetchUserInfo() async {
try {
final EMUserInfo? info = await IMManager.instance.getUserInfo(userId);
if (info != null) {
userInfo.value = info;
if (Get.isLogEnable) {
Get.log('获取用户信息成功: ${info.nickName}');
}
} else {
if (Get.isLogEnable) {
Get.log('未找到用户信息: $userId');
}
}
} catch (e) {
if (Get.isLogEnable) {
Get.log('获取用户信息失败: $e');
}
}
}
///
Future<bool> sendMessage(String content) async {
try {
final message = await IMManager.instance.sendTextMessage(content, userId);
return message != null;
} catch (e) {
if (Get.isLogEnable) {
Get.log('发送消息失败: $e');
}
return false;
}
}
}

90
lib/pages/message/chat_page.dart

@ -3,26 +3,26 @@ import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../controller/message/chat_controller.dart';
import '../../generated/assets.dart';
import '../../../widget/message/chat_input_bar.dart';
class ChatPage extends StatefulWidget {
class ChatPage extends StatelessWidget {
final String userId;
const ChatPage({required this.userId, super.key});
@override
State<ChatPage> createState() => _ChatPageState();
}
class _ChatPageState extends State<ChatPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
return GetBuilder<ChatController>(
init: ChatController(userId: userId),
builder: (controller) {
return Scaffold(
backgroundColor: Color(0xffF5F5F5),
appBar: AppBar(
title: Text("Chat"),
title: Text(controller.userInfo.value?.nickName ?? ''),
centerTitle: true,
actions: [
Container(
@ -43,75 +43,27 @@ class _ChatPageState extends State<ChatPage> {
children: [
//
Expanded(
child: SingleChildScrollView(
reverse: true,
padding: EdgeInsets.all(16.w),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
//
Text(
"昨天下午14:46",
style: TextStyle(
fontSize: 12.sp,
color: Colors.grey,
),
textAlign: TextAlign.center,
),
SizedBox(height: 16.h),
//
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
//
Container(
width: 40.w,
height: 40.w,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.w),
image: DecorationImage(
image: AssetImage(Assets.imagesAvatarsExample),
fit: BoxFit.cover,
),
),
),
SizedBox(width: 8.w),
//
Container(
padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(12.w),
bottomLeft: Radius.circular(12.w),
bottomRight: Radius.circular(12.w),
),
),
child: Text(
"你好",
style: TextStyle(
fontSize: 14.sp,
color: Colors.black,
),
),
),
],
),
],
child: Container(
alignment: Alignment.topCenter,
child: ListView.builder(
reverse: true,
itemCount: 10,
itemBuilder: (context, index) {
return Container();
},
),
),
),
// 使
ChatInputBar(
onSendMessage: (message) {
//
if (Get.isLogEnable) {
Get.log("接收到消息: $message");
}
onSendMessage: (message) async {
await controller.sendMessage(message);
},
),
],
),
);
},
);
}
}

100
lib/widget/message/chat_input_bar.dart

@ -0,0 +1,100 @@
import 'package:dating_touchme_app/extension/ex_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../generated/assets.dart';
class ChatInputBar extends StatefulWidget {
final ValueChanged<String> onSendMessage;
const ChatInputBar({required this.onSendMessage, super.key});
@override
State<ChatInputBar> createState() => _ChatInputBarState();
}
class _ChatInputBarState extends State<ChatInputBar> {
final TextEditingController _textController = TextEditingController();
void _handleSendMessage() {
if (_textController.text.isNotEmpty) {
// 使GetX日志而不是print
if (Get.isLogEnable) {
Get.log("发送消息: ${_textController.text}");
}
widget.onSendMessage(_textController.text);
_textController.clear();
}
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 16.w),
color: Colors.white,
child: Column(
children: [
SizedBox(height: 10.h),
Row(
children: [
Expanded(
child: Container(
height: 40.h,
decoration: BoxDecoration(
color: Color(0xffF5F5F5),
borderRadius: BorderRadius.circular(5.h),
),
padding: EdgeInsets.symmetric(horizontal: 16.w),
child: TextField(
controller: _textController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "请输入聊天内容~",
hintStyle: TextStyle(fontSize: 14.sp, color: Colors.grey),
),
style: TextStyle(fontSize: 14.sp, color: Colors.black),
),
),
),
SizedBox(width: 12.w),
//
Container(
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(5.h),
),
child: Text(
"发送",
style: TextStyle(
fontSize: 14.sp,
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
).onTap(_handleSendMessage),
],
),
SizedBox(height: 12.h),
//
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
//
Image.asset(Assets.imagesAudio, width: 24.w, height: 24.w),
//
Image.asset(Assets.imagesVideo, width: 24.w, height: 24.w),
//
Image.asset(Assets.imagesGift, width: 24.w, height: 24.w),
//
Image.asset(Assets.imagesEmoji, width: 24.w, height: 24.w),
//
Image.asset(Assets.imagesAdd, width: 24.w, height: 24.w),
],
),
],
),
);
}
}
Loading…
Cancel
Save