import 'package:dating_touchme_app/pages/message/chat_page.dart'; import 'package:flutter/material.dart'; import 'package:dating_touchme_app/generated/assets.dart'; import 'package:get/get.dart'; import 'package:tdesign_flutter/tdesign_flutter.dart'; import 'package:im_flutter_sdk/im_flutter_sdk.dart'; import '../../controller/message/conversation_controller.dart'; class ConversationTab extends StatefulWidget { const ConversationTab({super.key}); @override State createState() => _ConversationTabState(); } class _ConversationTabState extends State with AutomaticKeepAliveClientMixin { final ConversationController controller = Get.find(); @override Widget build(BuildContext context) { super.build(context); return Column( children: [ // 聊天列表 Expanded( child: Obx(() { 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), ElevatedButton( onPressed: () => controller.refreshConversations(), child: const Text('重试'), ), ], ), ); } return ListView.builder( padding: const EdgeInsets.only(top: 8), itemCount: controller.conversations.length, itemBuilder: (context, index) { final conversation = controller.conversations[index]; return _buildConversationItem(conversation); }, ); }), ), ], ); } // 构建会话项 Widget _buildConversationItem(EMConversation conversation) { // 优先从缓存获取用户信息,避免闪烁 final cachedUserInfo = controller.getCachedUserInfo(conversation.id); return FutureBuilder( future: cachedUserInfo != null ? Future.value(cachedUserInfo) : controller.loadContact(conversation.id), initialData: cachedUserInfo, builder: (context, userSnapshot) { final ExtendedUserInfo? userInfo = userSnapshot.data; return FutureBuilder( future: controller.lastMessage(conversation), builder: (context, messageSnapshot) { final EMMessage? message = messageSnapshot.data; return FutureBuilder( future: controller.getUnreadCount(conversation), builder: (context, unreadSnapshot) { final int unreadCount = unreadSnapshot.data ?? 0; final double screenWidth = MediaQuery.of(context).size.width; final Widget cellContent = Builder( builder: (cellContext) => GestureDetector( onTap: () async { TDSwipeCellInherited.of(cellContext)?.cellClick(); Get.to(ChatPage(userId: conversation.id)); }, child: Container( padding: const EdgeInsets.symmetric( horizontal: 16, vertical: 12, ), decoration: const BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(16)), ), margin: const EdgeInsets.only( bottom: 8, left: 16, right: 16, ), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( width: 56, height: 56, decoration: BoxDecoration( borderRadius: BorderRadius.circular(28), image: (userInfo?.avatarUrl != null && userInfo!.avatarUrl!.isNotEmpty) ? DecorationImage( image: NetworkImage(userInfo.avatarUrl!), fit: BoxFit.cover, ) : null, color: (userInfo?.avatarUrl == null || (userInfo?.avatarUrl?.isEmpty ?? true)) ? Colors.grey[300] : null, ), child: (userInfo?.avatarUrl == null || (userInfo?.avatarUrl?.isEmpty ?? true)) ? ClipRRect( borderRadius: BorderRadius.circular(28), child: Image.asset( Assets.imagesAvatarsExample, width: 56, height: 56, fit: BoxFit.cover, ), ) : null, ), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Text( userInfo?.nickName ?? conversation.id, // 如果没有昵称,显示用户ID style: const TextStyle( fontSize: 16, fontWeight: FontWeight.w500, color: Colors.black, ), ), ), Text( controller.formatMessageTime( message?.serverTime ?? 0, ), style: const TextStyle( fontSize: 12, color: Colors.grey, ), ), ], ), const SizedBox(height: 6), Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Expanded( child: Text( controller.getLastMessageContent(message), style: TextStyle( fontSize: 14, color: (message != null && message.direction == MessageDirection.SEND && message.status == MessageStatus.FAIL) ? Color.fromRGBO(248, 85, 66, 1) // 发送失败时显示红色 : Colors.grey, ), overflow: TextOverflow.ellipsis, ), ), if (unreadCount > 0) Container( margin: const EdgeInsets.only(left: 8), padding: const EdgeInsets.symmetric( horizontal: 8, vertical: 2, ), decoration: BoxDecoration( color: Colors.red, borderRadius: BorderRadius.circular(10), ), child: Text( unreadCount.toString(), style: const TextStyle( fontSize: 12, color: Colors.white, ), ), ), ], ), ], ), ), ], ), ), ), ); return TDSwipeCell( slidableKey: ValueKey('conversation_${conversation.id}'), groupTag: 'conversation_swipe_group', right: TDSwipeCellPanel( extentRatio: 72 / screenWidth, children: [ TDSwipeCellAction( backgroundColor: TDTheme.of(context).errorColor6, label: '删除', onPressed: (actionContext) async { final success = await controller.deleteConversation( conversation.id, ); if (!success) { Get.snackbar('提示', '删除会话失败'); } }, ), ], ), cell: cellContent, ); }, ); }, ); }, ); } @override bool get wantKeepAlive => true; }