2025-08-26 17:38:59 +08:00
|
|
|
/// 聊天首页模板
|
|
|
|
library;
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:loopin/IM/im_service.dart';
|
|
|
|
import 'package:loopin/behavior/custom_scroll_behavior.dart';
|
|
|
|
import 'package:loopin/components/network_or_asset_image.dart';
|
|
|
|
import 'package:loopin/models/conversation_type.dart';
|
2025-08-27 23:26:29 +08:00
|
|
|
import 'package:loopin/pages/chat/notify_controller/notify_no_friend_controller.dart';
|
2025-08-26 17:38:59 +08:00
|
|
|
import 'package:loopin/styles/index.dart';
|
|
|
|
import 'package:loopin/utils/index.dart';
|
2025-08-27 23:26:29 +08:00
|
|
|
import 'package:loopin/utils/parse_message_summary.dart';
|
2025-08-26 17:38:59 +08:00
|
|
|
import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation_filter.dart';
|
|
|
|
|
|
|
|
// noFriend, //陌生人消息
|
|
|
|
|
|
|
|
class Nofriend extends StatefulWidget {
|
|
|
|
const Nofriend({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<Nofriend> createState() => NofriendState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class NofriendState extends State<Nofriend> with SingleTickerProviderStateMixin {
|
|
|
|
bool isLoading = false; // 是否在加载中
|
|
|
|
bool hasMore = true; // 是否还有更多数据
|
|
|
|
final RxBool _throttleFlag = false.obs; // 滚动节流锁
|
|
|
|
final ScrollController chatController = ScrollController();
|
2025-08-27 23:26:29 +08:00
|
|
|
int page = 0;
|
2025-08-26 17:38:59 +08:00
|
|
|
|
|
|
|
///-------------------
|
2025-08-27 23:26:29 +08:00
|
|
|
final dataController = Get.put<NotifyNoFriendController>(NotifyNoFriendController());
|
2025-08-26 17:38:59 +08:00
|
|
|
|
|
|
|
RxList demoList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].obs;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
getMsgData();
|
|
|
|
chatController.addListener(() {
|
|
|
|
if (_throttleFlag.value) return;
|
|
|
|
if (chatController.position.pixels >= chatController.position.maxScrollExtent - 50) {
|
|
|
|
_throttleFlag.value = true;
|
|
|
|
getMsgData().then((_) {
|
|
|
|
// 解锁
|
|
|
|
Future.delayed(Duration(milliseconds: 1000), () {
|
|
|
|
_throttleFlag.value = false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2025-08-27 23:26:29 +08:00
|
|
|
logger.e('dispose');
|
|
|
|
Get.delete<NotifyNoFriendController>();
|
2025-08-26 17:38:59 +08:00
|
|
|
chatController.dispose();
|
2025-08-27 23:26:29 +08:00
|
|
|
super.dispose();
|
2025-08-26 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2025-08-27 23:26:29 +08:00
|
|
|
// 分页获取陌生人会话
|
2025-08-26 17:38:59 +08:00
|
|
|
Future<void> getMsgData() async {
|
|
|
|
final res = await ImService.instance.getConversationListByFilter(
|
|
|
|
filter: V2TimConversationFilter(conversationGroup: ConversationType.noFriend.name),
|
|
|
|
nextSeq: page,
|
|
|
|
);
|
2025-08-27 23:26:29 +08:00
|
|
|
logger.i('获取会话数据成功:${res.data!.toLogString()}');
|
2025-08-26 17:38:59 +08:00
|
|
|
if (res.success && res.data != null) {
|
|
|
|
final newList = res.data!.conversationList ?? [];
|
2025-08-27 23:26:29 +08:00
|
|
|
// 已有会话ID集合
|
|
|
|
final existingIds = dataController.convList.map((e) => e.conversationID).where((id) => id.isNotEmpty).toSet();
|
|
|
|
// 过滤掉已存在的会话
|
|
|
|
final filtered = newList.where((c) => !existingIds.contains(c.conversationID)).toList();
|
|
|
|
|
2025-08-26 17:38:59 +08:00
|
|
|
final isFinished = res.data!.isFinished ?? true;
|
2025-08-27 23:26:29 +08:00
|
|
|
dataController.convList.addAll(filtered);
|
2025-08-26 17:38:59 +08:00
|
|
|
if (isFinished) {
|
|
|
|
hasMore = false;
|
|
|
|
// 加载没数据了
|
2025-08-27 23:26:29 +08:00
|
|
|
page = 0;
|
2025-08-26 17:38:59 +08:00
|
|
|
} else {
|
2025-08-27 23:26:29 +08:00
|
|
|
page = int.parse(res.data!.nextSeq ?? '0');
|
2025-08-26 17:38:59 +08:00
|
|
|
}
|
|
|
|
logger.i('获取会话数据成功:$newList');
|
|
|
|
} else {
|
|
|
|
logger.e('获取会话数据失败:${res.data!.isFinished}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 下拉刷新
|
|
|
|
Future<void> handleRefresh() async {
|
|
|
|
await Future.delayed(Duration(seconds: 5));
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
backgroundColor: Colors.grey[50],
|
|
|
|
appBar: AppBar(
|
|
|
|
centerTitle: true,
|
|
|
|
forceMaterialTransparency: true,
|
|
|
|
bottom: PreferredSize(
|
|
|
|
preferredSize: Size.fromHeight(1.0),
|
|
|
|
child: Container(
|
|
|
|
color: Colors.grey[300],
|
|
|
|
height: 1.0,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
title: Text(
|
|
|
|
'陌生人消息',
|
2025-09-03 11:25:31 +08:00
|
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
2025-08-26 17:38:59 +08:00
|
|
|
),
|
|
|
|
actions: [],
|
|
|
|
),
|
|
|
|
body: ScrollConfiguration(
|
|
|
|
behavior: CustomScrollBehavior().copyWith(scrollbars: false),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: RefreshIndicator(
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
color: Color(0xFFFF5000),
|
|
|
|
displacement: 10.0,
|
|
|
|
onRefresh: handleRefresh,
|
|
|
|
child: Obx(() {
|
2025-08-27 23:26:29 +08:00
|
|
|
// final chatList = controller.chatList;
|
|
|
|
|
2025-08-26 17:38:59 +08:00
|
|
|
return ListView.builder(
|
|
|
|
shrinkWrap: true,
|
|
|
|
physics: BouncingScrollPhysics(),
|
2025-08-27 23:26:29 +08:00
|
|
|
itemCount: dataController.convList.length,
|
2025-08-26 17:38:59 +08:00
|
|
|
itemBuilder: (context, index) {
|
2025-08-27 23:26:29 +08:00
|
|
|
final conv = dataController.convList[index];
|
|
|
|
final isNoFriend = conv.conversationGroupList?.contains(ConversationType.noFriend.name) ?? false;
|
|
|
|
|
|
|
|
return Ink(
|
2025-09-03 11:25:31 +08:00
|
|
|
key: ValueKey(conv.conversationID),
|
|
|
|
|
2025-08-27 23:26:29 +08:00
|
|
|
// color: conv['topMost'] == null ? Colors.white : Colors.grey[100], //置顶颜色
|
|
|
|
child: InkWell(
|
|
|
|
splashColor: Colors.grey[200],
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
|
|
|
|
child: Row(
|
|
|
|
spacing: 10.0,
|
|
|
|
children: <Widget>[
|
|
|
|
// 头图
|
|
|
|
ClipOval(
|
|
|
|
child: NetworkOrAssetImage(
|
|
|
|
imageUrl: conv.faceUrl,
|
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
),
|
2025-08-26 17:38:59 +08:00
|
|
|
),
|
|
|
|
|
2025-08-27 23:26:29 +08:00
|
|
|
// 消息
|
|
|
|
Expanded(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: <Widget>[
|
|
|
|
Text(
|
|
|
|
conv.showName ?? '未知',
|
|
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.normal),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 2.0),
|
|
|
|
Text(
|
|
|
|
conv.lastMessage != null ? parseMessageSummary(conv.lastMessage!) : '',
|
|
|
|
style: const TextStyle(color: Colors.grey, fontSize: 13.0),
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
// 右侧
|
|
|
|
|
|
|
|
Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
2025-08-26 17:38:59 +08:00
|
|
|
children: <Widget>[
|
2025-08-27 23:26:29 +08:00
|
|
|
Visibility(
|
|
|
|
visible: true,
|
|
|
|
child: Text(
|
|
|
|
// 转成日期字符串显示
|
|
|
|
Utils.formatTime(conv.lastMessage!.timestamp ?? DateTime.now().millisecondsSinceEpoch ~/ 1000),
|
|
|
|
style: const TextStyle(color: Colors.grey, fontSize: 12.0),
|
2025-08-26 17:38:59 +08:00
|
|
|
),
|
|
|
|
),
|
2025-08-27 23:26:29 +08:00
|
|
|
const SizedBox(height: 5.0),
|
|
|
|
// 数字角标
|
|
|
|
Visibility(
|
|
|
|
visible: (conv.unreadCount ?? 0) > 0,
|
|
|
|
child: FStyle.badge(conv.unreadCount ?? 0),
|
2025-08-26 17:38:59 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Visibility(
|
2025-08-27 23:26:29 +08:00
|
|
|
visible: false,
|
|
|
|
child: const Icon(
|
|
|
|
Icons.arrow_forward_ios,
|
|
|
|
color: Colors.blueGrey,
|
|
|
|
size: 14.0,
|
2025-08-26 17:38:59 +08:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2025-08-27 23:26:29 +08:00
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
Get.toNamed('/chatNoFriend', arguments: conv);
|
|
|
|
},
|
2025-08-26 17:38:59 +08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
})),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|