236 lines
9.5 KiB
Dart
236 lines
9.5 KiB
Dart
/// 聊天首页模板
|
||
library;
|
||
|
||
import 'dart:convert';
|
||
|
||
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';
|
||
import 'package:loopin/styles/index.dart';
|
||
import 'package:loopin/utils/index.dart';
|
||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation.dart';
|
||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation_filter.dart';
|
||
import 'package:tencent_cloud_chat_sdk/models/v2_tim_message.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();
|
||
int page = 1;
|
||
|
||
///-------------------
|
||
RxList<V2TimConversation> convList = <V2TimConversation>[].obs;
|
||
|
||
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() {
|
||
super.dispose();
|
||
chatController.dispose();
|
||
}
|
||
|
||
// 分页获取全部数据
|
||
Future<void> getMsgData() async {
|
||
final res = await ImService.instance.getConversationListByFilter(
|
||
filter: V2TimConversationFilter(conversationGroup: ConversationType.noFriend.name),
|
||
nextSeq: page,
|
||
);
|
||
logger.i('获取会话数据成功:${res.data!.toJson()}');
|
||
if (res.success && res.data != null) {
|
||
final newList = res.data!.conversationList ?? [];
|
||
final isFinished = res.data!.isFinished ?? true;
|
||
convList.addAll(newList);
|
||
if (isFinished) {
|
||
hasMore = false;
|
||
// 加载没数据了
|
||
page = int.parse(res.data!.nextSeq ?? '0');
|
||
} else {
|
||
page++;
|
||
}
|
||
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(
|
||
'陌生人消息',
|
||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
|
||
),
|
||
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(() {
|
||
return ListView.builder(
|
||
controller: chatController,
|
||
shrinkWrap: true,
|
||
physics: BouncingScrollPhysics(),
|
||
// itemCount: convList.length,
|
||
itemCount: demoList.length,
|
||
itemBuilder: (context, index) {
|
||
// ----测试数据
|
||
final jsonData = '{"faceUrl":"","nickName":"测试昵称","userID":"213213"}';
|
||
final item = jsonDecode(jsonData); // 数据
|
||
final desc = '测试desc';
|
||
final cloudCustomData = 'interactionLike';
|
||
V2TimMessage element = V2TimMessage(elemType: 2, isRead: index > 2 ? true : false);
|
||
return Container(
|
||
width: double.infinity,
|
||
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
|
||
child: Row(
|
||
spacing: 10.0,
|
||
children: <Widget>[
|
||
// 头像
|
||
InkWell(
|
||
onTap: () async {
|
||
// 点击头像转到对方主页
|
||
// 先获取视频详情
|
||
// 如果cloudCustomData是interactionComment,interactionAt,interactionReply,传参时带上评论id,
|
||
//
|
||
// final res = await Http.get('${VideoApi.detail}/${item['vlogID']}');
|
||
// Get.toNamed('/vloger', arguments: res['data']);
|
||
Get.toNamed('/vloger');
|
||
},
|
||
child: ClipOval(
|
||
child: NetworkOrAssetImage(
|
||
imageUrl: item['faceUrl'],
|
||
width: 50,
|
||
height: 50,
|
||
),
|
||
),
|
||
),
|
||
|
||
// 消息
|
||
Expanded(
|
||
child: InkWell(
|
||
onTap: () {
|
||
// 点击头像转到对应的聊天
|
||
},
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: <Widget>[
|
||
// 昵称
|
||
Text(
|
||
item['nickName'] ?? '未知',
|
||
style: TextStyle(
|
||
fontSize: 16,
|
||
fontWeight: FontWeight.normal,
|
||
),
|
||
),
|
||
const SizedBox(height: 2.0),
|
||
// 描述内容
|
||
Text(
|
||
maxLines: 2,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: const TextStyle(color: Colors.grey, fontSize: 12.0),
|
||
// desc,
|
||
'很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容很长文本内容',
|
||
),
|
||
Text(
|
||
Utils.formatTime(element.timestamp ?? DateTime.now().millisecondsSinceEpoch ~/ 1000),
|
||
style: TextStyle(
|
||
color: Colors.grey[600],
|
||
fontSize: 12,
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
|
||
// 右侧
|
||
Row(
|
||
crossAxisAlignment: CrossAxisAlignment.center,
|
||
children: <Widget>[
|
||
Visibility(
|
||
visible: true,
|
||
// 视频首图
|
||
child: NetworkOrAssetImage(
|
||
imageUrl: item['firstFrameImg'],
|
||
placeholderAsset: 'assets/images/bk.jpg',
|
||
width: 40,
|
||
height: 60,
|
||
),
|
||
),
|
||
const SizedBox(width: 5.0),
|
||
// 角标
|
||
Visibility(
|
||
visible: !(element.isRead ?? true),
|
||
child: FStyle.badge(0, isdot: true),
|
||
),
|
||
],
|
||
),
|
||
],
|
||
),
|
||
);
|
||
},
|
||
);
|
||
})),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|