From eba522dc817f6c2c89d5ad95f49203fd2fd9876b Mon Sep 17 00:00:00 2001 From: Jolie <412895109@qq.com> Date: Mon, 1 Dec 2025 16:28:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(message):=20=E5=AE=9E=E7=8E=B0=E5=A5=BD?= =?UTF-8?q?=E5=8F=8B=E5=88=97=E8=A1=A8=E4=BB=8EIM=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=B9=B6=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 GetX 和 FriendController 管理好友数据 - 使用 EMContact 替代模拟数据展示好友列表 - 添加加载状态与错误重试机制 - 新增 _buildFriendItemFromContact 方法构建好友项 - 废弃原有模拟数据列表 - IMManager 中新增 getAllContacts 方法获取联系人列表 - 优化聊天室页面 --- lib/controller/discover/room_controller.dart | 10 +- lib/im/im_manager.dart | 7 +- lib/pages/discover/live_room_page.dart | 4 + lib/pages/message/friend_tab.dart | 196 ++++++++++++++----- 4 files changed, 163 insertions(+), 54 deletions(-) diff --git a/lib/controller/discover/room_controller.dart b/lib/controller/discover/room_controller.dart index d8b3fb9..7be37ee 100644 --- a/lib/controller/discover/room_controller.dart +++ b/lib/controller/discover/room_controller.dart @@ -429,24 +429,20 @@ class RoomController extends GetxController with WidgetsBindingObserver { // 在公屏显示赠送礼物消息 final senderNickName = GlobalData().userData?.nickName ?? '用户'; String targetNickName = '用户'; - final targetUserIdStr = targetUserId.toString(); // 从频道详情中查找目标用户昵称 final channelDetail = rtcChannelDetail.value; if (channelDetail != null) { // 检查是否是主持人 - if (channelDetail.anchorInfo?.userId == targetUserIdStr || - channelDetail.anchorInfo?.miId == targetUserIdStr) { + if (channelDetail.anchorInfo?.uid == targetUserId) { targetNickName = channelDetail.anchorInfo?.nickName ?? '用户'; } // 检查是否是男嘉宾 - else if (channelDetail.maleInfo?.userId == targetUserIdStr || - channelDetail.maleInfo?.miId == targetUserIdStr) { + else if (channelDetail.maleInfo?.uid == targetUserId) { targetNickName = channelDetail.maleInfo?.nickName ?? '用户'; } // 检查是否是女嘉宾 - else if (channelDetail.femaleInfo?.userId == targetUserIdStr || - channelDetail.femaleInfo?.miId == targetUserIdStr) { + else if (channelDetail.femaleInfo?.uid == targetUserId) { targetNickName = channelDetail.femaleInfo?.nickName ?? '用户'; } } diff --git a/lib/im/im_manager.dart b/lib/im/im_manager.dart index a448fea..803cb06 100644 --- a/lib/im/im_manager.dart +++ b/lib/im/im_manager.dart @@ -442,9 +442,14 @@ class IMManager { return await EMClient.getInstance.chatManager.sendMessage(customMsg); } + /// 获取所有联系人 + Future> getAllContacts() async { + return await EMClient.getInstance.contactManager.fetchAllContacts(); + } + /// 获取会话列表 Future> getConversations() async { - return EMClient.getInstance.chatManager.loadAllConversations(); + return await EMClient.getInstance.chatManager.loadAllConversations(); } /// 获取用户信息(单个用户) diff --git a/lib/pages/discover/live_room_page.dart b/lib/pages/discover/live_room_page.dart index 16a7d01..9c4cc4c 100644 --- a/lib/pages/discover/live_room_page.dart +++ b/lib/pages/discover/live_room_page.dart @@ -73,6 +73,8 @@ class _LiveRoomPageState extends State { } void _showGiftPopup() { + // 隐藏键盘 + FocusScope.of(context).unfocus(); SmartDialog.show( alignment: Alignment.bottomCenter, maskColor: TDTheme.of(context).fontGyColor2, @@ -92,6 +94,8 @@ class _LiveRoomPageState extends State { } void _showRechargePopup() { + // 隐藏键盘 + FocusScope.of(context).unfocus(); SmartDialog.show( alignment: Alignment.bottomCenter, maskColor: TDTheme.of(context).fontGyColor2, diff --git a/lib/pages/message/friend_tab.dart b/lib/pages/message/friend_tab.dart index c26ecd1..39a7259 100644 --- a/lib/pages/message/friend_tab.dart +++ b/lib/pages/message/friend_tab.dart @@ -1,5 +1,8 @@ import 'package:flutter/material.dart'; import 'package:dating_touchme_app/generated/assets.dart'; +import 'package:get/get.dart'; +import 'package:dating_touchme_app/controller/message/friend_controller.dart'; +import 'package:im_flutter_sdk/im_flutter_sdk.dart'; class FriendTab extends StatefulWidget { const FriendTab({super.key}); @@ -45,45 +48,45 @@ class _FriendTabState extends State with TickerProviderStateMixin { }, ]; - // 模拟数据 - 好友列表 - final List> _friendList = [ - { - "id": 1, - "name": "林园园", - "avatar": Assets.imagesAvatarsExample, - "isOnline": true, - }, - { - "id": 2, - "name": "李晖", - "avatar": Assets.imagesAvatarsExample, - "isOnline": false, - }, - { - "id": 3, - "name": "李哲", - "avatar": Assets.imagesAvatarsExample, - "isOnline": false, - }, - { - "id": 4, - "name": "李夏", - "avatar": Assets.imagesAvatarsExample, - "isOnline": true, - }, - { - "id": 5, - "name": "张雪", - "avatar": Assets.imagesAvatarsExample, - "isOnline": false, - }, - { - "id": 6, - "name": "王强", - "avatar": Assets.imagesAvatarsExample, - "isOnline": true, - }, - ]; + // 模拟数据 - 好友列表(已废弃,现在使用 FriendController 从 IM 获取) + // final List> _friendList = [ + // { + // "id": 1, + // "name": "林园园", + // "avatar": Assets.imagesAvatarsExample, + // "isOnline": true, + // }, + // { + // "id": 2, + // "name": "李晖", + // "avatar": Assets.imagesAvatarsExample, + // "isOnline": false, + // }, + // { + // "id": 3, + // "name": "李哲", + // "avatar": Assets.imagesAvatarsExample, + // "isOnline": false, + // }, + // { + // "id": 4, + // "name": "李夏", + // "avatar": Assets.imagesAvatarsExample, + // "isOnline": true, + // }, + // { + // "id": 5, + // "name": "张雪", + // "avatar": Assets.imagesAvatarsExample, + // "isOnline": false, + // }, + // { + // "id": 6, + // "name": "王强", + // "avatar": Assets.imagesAvatarsExample, + // "isOnline": true, + // }, + // ]; // 模拟数据 - 粉丝列表 final List> _fansList = [ @@ -301,17 +304,56 @@ class _FriendTabState extends State with TickerProviderStateMixin { // 构建好友列表 Widget _buildFriendList() { - return ListView.builder( - padding: const EdgeInsets.only(top: 8), - itemCount: _friendList.length, - itemBuilder: (context, index) { - final friend = _friendList[index]; - return _buildFriendItem(friend); + return GetX( + init: FriendController(), + builder: (controller) { + if (controller.isLoading.value) { + return const Center( + child: CircularProgressIndicator(), + ); + } + + if (controller.errorMessage.value.isNotEmpty) { + return Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + controller.errorMessage.value, + style: const TextStyle(color: Colors.grey), + ), + const SizedBox(height: 16), + ElevatedButton( + onPressed: () => controller.refreshFriends(), + child: const Text('重试'), + ), + ], + ), + ); + } + + if (controller.friendList.isEmpty) { + return const Center( + child: Text( + '暂无好友', + style: TextStyle(color: Colors.grey), + ), + ); + } + + return ListView.builder( + padding: const EdgeInsets.only(top: 8), + itemCount: controller.friendList.length, + itemBuilder: (context, index) { + final contact = controller.friendList[index]; + return _buildFriendItemFromContact(contact); + }, + ); }, ); } - // 构建好友项 + // 构建好友项(从 Map 数据,保留用于兼容) Widget _buildFriendItem(Map friend) { return Container( margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4), @@ -387,6 +429,68 @@ class _FriendTabState extends State with TickerProviderStateMixin { ); } + // 构建好友项(从 EMContact 数据) + Widget _buildFriendItemFromContact(EMContact contact) { + // EMContact 只有 userId,需要通过其他方式获取详细信息 + // 这里先显示 userId,后续可以通过 getUserInfo 获取详细信息 + final userId = contact.userId; + + return Container( + margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4), + padding: const EdgeInsets.all(12), + decoration: BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.circular(8), + ), + child: Row( + children: [ + // 头像 + Container( + width: 50, + height: 50, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(25), + ), + child: ClipRRect( + borderRadius: BorderRadius.circular(25), + child: const Icon(Icons.person, size: 30), + ), + ), + + // 信息 + Expanded( + child: Container( + margin: const EdgeInsets.only(left: 12), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + userId, + style: const TextStyle( + fontWeight: FontWeight.bold, + ), + ), + const SizedBox(height: 4), + Text( + "ID: $userId", + style: const TextStyle( + color: Colors.grey, + fontSize: 12, + ), + ), + ], + ), + ), + ), + + // 箭头图标 + const Icon(Icons.arrow_forward_ios, size: 16, color: Colors.grey), + ], + ), + ); + } + // 构建粉丝列表 Widget _buildFansList() { return ListView.builder(