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.
 
 
 
 
 

247 lines
9.7 KiB

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<ConversationTab> createState() => _ConversationTabState();
}
class _ConversationTabState extends State<ConversationTab> with AutomaticKeepAliveClientMixin {
final ConversationController controller = Get.find<ConversationController>();
// 模拟数据 - 顶部推荐用户列表
final List<Map<String, dynamic>> _recommendedUsers = [
{"id": 1, "avatar": Assets.imagesAvatarsExample, "type": "相亲"},
{"id": 2, "avatar": Assets.imagesAvatarsExample, "type": "交友"},
{"id": 3, "avatar": Assets.imagesAvatarsExample, "type": "相亲"},
{"id": 4, "avatar": Assets.imagesAvatarsExample, "type": "相亲"},
{"id": 5, "avatar": Assets.imagesAvatarsExample, "type": "相亲"},
{"id": 6, "avatar": Assets.imagesAvatarsExample, "type": "相亲"},
];
@override
Widget build(BuildContext context) {
super.build(context);
return Column(
children: [
// 推荐用户横向滚动列表
_buildRecommendedUsers(),
// 聊天列表
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 _buildRecommendedUsers() {
return SizedBox(
height: 60,
child: ListView.builder(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 16),
itemCount: _recommendedUsers.length,
itemBuilder: (context, index) {
final user = _recommendedUsers[index];
final bool isSelected = index == 1; // 模拟选中状态
return Container(
margin: const EdgeInsets.only(right: 12),
child: Column(
children: [
Stack(
children: [
Container(
width: 60,
height: 60,
decoration: BoxDecoration(
border: Border.all(
color: isSelected ? Colors.blue : Colors.transparent,
width: 2,
),
borderRadius: BorderRadius.circular(30),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(30),
child: Image.asset(user["avatar"], fit: BoxFit.cover),
),
),
if (user["type"] == "交友")
Positioned(
bottom: 0,
right: 0,
child: Container(
width: 20,
height: 20,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(10),
),
child: const Icon(
Icons.people,
size: 12,
color: Colors.white,
),
),
),
],
),
],
),
);
},
),
);
}
// 构建会话项
Widget _buildConversationItem(EMConversation conversation) {
// 使用FutureBuilder获取未读消息数和最新消息
return FutureBuilder<EMUserInfo>(
future: controller.loadContact(conversation.id),
builder: (context, snapshot) {
EMUserInfo? userInfo = snapshot.data;
return FutureBuilder<EMMessage?>(
future: controller.lastMessage(conversation),
builder: (context, snapshot) {
EMMessage? message = snapshot.data;
return FutureBuilder<int>(
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;
}