import 'package:dating_touchme_app/extension/ex_widget.dart'; import 'package:dating_touchme_app/im/im_manager.dart'; 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: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('重试'), ), ], ), ); } if (controller.conversations.isEmpty) { return const Center(child: 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) { // 使用FutureBuilder获取未读消息数和最新消息 return FutureBuilder( future: controller.loadContact(conversation.id), builder: (context, snapshot) { EMUserInfo? userInfo = snapshot.data; return FutureBuilder( future: controller.lastMessage(conversation), builder: (context, snapshot) { EMMessage? message = snapshot.data; return FutureBuilder( future: controller.getUnreadCount(conversation), builder: (context, snapshot){ int unreadCount = snapshot.data ?? 0; return GestureDetector( onTap: () async{ 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: DecorationImage( image: userInfo?.avatarUrl != '' ? NetworkImage(userInfo?.avatarUrl ?? '') : AssetImage(Assets.imagesAvatarsExample), fit: BoxFit.cover, ), ), ), const SizedBox(width: 12), // 会话信息 Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( userInfo?.nickName ?? '联系人', 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: 4), Text( controller.getLastMessageContent(message), style: const TextStyle( fontSize: 14, color: Colors.grey, ), overflow: TextOverflow.ellipsis, ), ], ), ), // 未读消息数 if (unreadCount > 0) Container( 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, ), ), ), ], ), ), ); } ); }, ); }, ); } @override bool get wantKeepAlive => true; }