From 4eb3dd12785249b18bd1f85af9eab5d87900e2c7 Mon Sep 17 00:00:00 2001 From: Jolie <412895109@qq.com> Date: Tue, 11 Nov 2025 16:04:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(message):=20=E5=AE=9E=E7=8E=B0=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E9=A1=B5=E9=9D=A2=E5=9F=BA=E7=A1=80=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 ChatController 管理聊天逻辑 - 实现用户信息获取与消息发送功能 - 创建 ChatInputBar 组件处理消息输入 - 更新 ChatPage 使用 GetBuilder 构建界面 - 集成 IMManager 处理即时通讯逻辑 - 添加消息列表基础布局与空内容展示 - 实现发送按钮交互与文本输入控制 --- lib/controller/message/chat_controller.dart | 54 +++++++++++ lib/pages/message/chat_page.dart | 90 ++++-------------- lib/widget/message/chat_input_bar.dart | 100 ++++++++++++++++++++ 3 files changed, 175 insertions(+), 69 deletions(-) create mode 100644 lib/controller/message/chat_controller.dart create mode 100644 lib/widget/message/chat_input_bar.dart diff --git a/lib/controller/message/chat_controller.dart b/lib/controller/message/chat_controller.dart new file mode 100644 index 0000000..270d404 --- /dev/null +++ b/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 userInfo = Rx(null); + + ChatController({required this.userId}); + + @override + void onInit() { + super.onInit(); + // 初始化时获取用户信息 + fetchUserInfo(); + } + + /// 获取用户信息 + Future 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 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; + } + } +} \ No newline at end of file diff --git a/lib/pages/message/chat_page.dart b/lib/pages/message/chat_page.dart index 14c23fe..dbd219b 100644 --- a/lib/pages/message/chat_page.dart +++ b/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 createState() => _ChatPageState(); -} - -class _ChatPageState extends State { @override Widget build(BuildContext context) { - return Scaffold( + return GetBuilder( + 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 { 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); }, ), ], ), ); + }, + ); } } diff --git a/lib/widget/message/chat_input_bar.dart b/lib/widget/message/chat_input_bar.dart new file mode 100644 index 0000000..ec219eb --- /dev/null +++ b/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 onSendMessage; + + const ChatInputBar({required this.onSendMessage, super.key}); + + @override + State createState() => _ChatInputBarState(); +} + +class _ChatInputBarState extends State { + 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), + ], + ), + ], + ), + ); + } +}