Browse Source

feat(message): 实现好友列表从IM获取并展示

- 引入 GetX 和 FriendController 管理好友数据
- 使用 EMContact 替代模拟数据展示好友列表
- 添加加载状态与错误重试机制
- 新增 _buildFriendItemFromContact 方法构建好友项
- 废弃原有模拟数据列表
- IMManager 中新增 getAllContacts 方法获取联系人列表
- 优化聊天室页面
ios
Jolie 4 months ago
parent
commit
eba522dc81
4 changed files with 163 additions and 54 deletions
  1. 10
      lib/controller/discover/room_controller.dart
  2. 7
      lib/im/im_manager.dart
  3. 4
      lib/pages/discover/live_room_page.dart
  4. 196
      lib/pages/message/friend_tab.dart

10
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 ?? '用户';
}
}

7
lib/im/im_manager.dart

@ -442,9 +442,14 @@ class IMManager {
return await EMClient.getInstance.chatManager.sendMessage(customMsg);
}
///
Future<List<EMContact>> getAllContacts() async {
return await EMClient.getInstance.contactManager.fetchAllContacts();
}
///
Future<List<EMConversation>> getConversations() async {
return EMClient.getInstance.chatManager.loadAllConversations();
return await EMClient.getInstance.chatManager.loadAllConversations();
}
///

4
lib/pages/discover/live_room_page.dart

@ -73,6 +73,8 @@ class _LiveRoomPageState extends State<LiveRoomPage> {
}
void _showGiftPopup() {
//
FocusScope.of(context).unfocus();
SmartDialog.show(
alignment: Alignment.bottomCenter,
maskColor: TDTheme.of(context).fontGyColor2,
@ -92,6 +94,8 @@ class _LiveRoomPageState extends State<LiveRoomPage> {
}
void _showRechargePopup() {
//
FocusScope.of(context).unfocus();
SmartDialog.show(
alignment: Alignment.bottomCenter,
maskColor: TDTheme.of(context).fontGyColor2,

196
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<FriendTab> with TickerProviderStateMixin {
},
];
// -
final List<Map<String, dynamic>> _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<Map<String, dynamic>> _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<Map<String, dynamic>> _fansList = [
@ -301,17 +304,56 @@ class _FriendTabState extends State<FriendTab> 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<FriendController>(
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<String, dynamic> friend) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
@ -387,6 +429,68 @@ class _FriendTabState extends State<FriendTab> 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(

Loading…
Cancel
Save