You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

225 lines
9.4 KiB

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<ConversationTab> createState() => _ConversationTabState();
}
class _ConversationTabState extends State<ConversationTab>
with AutomaticKeepAliveClientMixin {
final ConversationController controller = Get.find<ConversationController>();
@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) {
return FutureBuilder<EMUserInfo?>(
future: controller.loadContact(conversation.id),
builder: (context, userSnapshot) {
final EMUserInfo? userInfo = userSnapshot.data;
return FutureBuilder<EMMessage?>(
future: controller.lastMessage(conversation),
builder: (context, messageSnapshot) {
final EMMessage? message = messageSnapshot.data;
return FutureBuilder<int>(
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: DecorationImage(
image: (userInfo?.avatarUrl ?? '').isNotEmpty
? NetworkImage(userInfo!.avatarUrl!)
: const AssetImage(
Assets.imagesAvatarsExample,
)
as ImageProvider,
fit: BoxFit.cover,
),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Text(
(userInfo?.nickName ?? '').isNotEmpty
? 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: const TextStyle(
fontSize: 14,
color: 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;
}