import 'dart:async'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_slidable/flutter_slidable.dart'; import 'package:intl/intl.dart'; import 'package:talk/beans/message_bean.dart'; import 'package:talk/tools/message/message_model.dart'; import '../../common/func.dart'; import '../../dialog/restart_chat_dialog.dart'; import '../chat/chat_page.dart'; ///消息列表 class MessagePage extends StatefulWidget { const MessagePage({super.key}); @override State createState() => _MessagePageState(); } class _MessagePageState extends State { List messageList = []; late StreamSubscription subscription; final MessageModel _viewmodel = MessageModel(); int delIndex = 0; @override void initState() { // TODO: implement initState super.initState(); subscription = _viewmodel.streamController.stream.listen((newData) { String code = newData['code']; if (code.isNotEmpty) { switch (code) { case "getChatHistoryList": messageList = newData['data']; break; case "delChat": //重启对话 if (newData['data']) { messageList.removeAt(delIndex); } break; default: EasyLoading.showToast(newData['data']); break; } EasyLoading.dismiss(); setState(() {}); } }); loadData(); } loadData() { EasyLoading.show(status: 'loading...'); _viewmodel.getChatHistoryList(); } @override void dispose() { // TODO: implement dispose subscription.cancel(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color(0xFF121213), body: Stack( children: [ Column( children: [ Container( margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top + 9, left: 16, right: 16), alignment: Alignment.centerLeft, child: Text( '消息列表', style: TextStyle(color: Color(0xFFFF9000), fontSize: 16), ), ), Expanded( child: SlidableAutoCloseBehavior( child: ListView.builder( itemCount: messageList.length, itemBuilder: (BuildContext context, int index) { return _item(messageList[index], context, index); }), )) ], ) ], ), ); } _item(MessageBean data, BuildContext context, index) { final size = MediaQuery.of(context).size; final h50 = size.width / 7.2; final b20 = size.width / 18; final w40 = size.width / 9; final l68 = size.width / 5.2941176470588; final t25 = size.width / 14.4; final w160 = size.width / 2.25; return Slidable( //左滑划出的菜单 endActionPane: ActionPane( key: Key(UniqueKey().toString()), // 菜单宽度 extentRatio: 0.3, // dragDismissible: false, // 滑动动效 // DrawerMotion() StretchMotion() // motion: ScrollMotion(), // A pane can dismiss the Slidable. motion: BehindMotion(), children: [ SlidableAction( onPressed: (BuildContext context) { FunctionUtil.popDialog(context, RestartChatDialog( onTap: () { delIndex = index; EasyLoading.show(status: 'loading...'); _viewmodel.delChat(data.id); }, )); }, backgroundColor: Color(0xFFFF9000), foregroundColor: Colors.black, label: '删除', ), ], ), child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => ChatPage( characterId: data.id.toString(), )), ); }, child: Container( height: h50, margin: EdgeInsets.only(bottom: b20), child: Stack( alignment: Alignment.center, children: [ ///头像 Positioned( left: 16, child: ClipOval( child: CachedNetworkImage( width: w40, height: w40, imageUrl: data.iconImage!, errorWidget: (context, url, error) => const Icon(Icons.error), ), ), ), Positioned( left: l68, top: 6, child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( data.name!, style: TextStyle(fontSize: 13, color: Color(0xFFE1E1E1)), ), data.isMemoryCard! ? Container( width: 45, height: 10, margin: EdgeInsets.only(left: 10), alignment: Alignment.center, decoration: BoxDecoration( gradient: LinearGradient( colors: [Color(0xFF8F79F2), Color(0xFFE47BFD)], // 三色渐变数组 begin: Alignment.centerLeft, // 渐变开始位置 end: Alignment.centerRight, // 渐变结束位置 ), borderRadius: BorderRadius.all(Radius.circular(10))), child: Text( "记忆提升中", style: TextStyle(color: Colors.white, fontSize: 6), ), ) : Container(), ], )), Positioned( left: l68, top: t25, child: Container( width: w160, child: Text( '${data.lastMessage}', maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 12, color: Color(0xFF777777)), ), )), Positioned( right: 16, top: 6, child: Container( child: Text( '${getHM(data.lastContactTime)}', style: TextStyle(fontSize: 10, color: Color(0xFF8D8D8D)), ), )), ], ), ), ), ); } doNothing(BuildContext context, id, index) { delIndex = index; EasyLoading.show(status: 'loading...'); _viewmodel.delChat(id); EasyLoading.showToast("status"); } ///提取时间 getHM(dateTimeString) { DateTime inputDateTime = DateTime.parse(dateTimeString); DateTime now = DateTime.now(); bool isToday = inputDateTime.year == now.year && inputDateTime.month == now.month && inputDateTime.day == now.day; bool isThisYear = inputDateTime.year == now.year; String formattedDateTime; if (isToday) { // 当天内,只显示【时:分】 final DateFormat timeFormatter = DateFormat('HH:mm'); formattedDateTime = timeFormatter.format(inputDateTime); } else if (isThisYear) { // 超出当天但仍在今年内,显示【月-日 时:分】 final DateFormat dateFormatter = DateFormat('MM-dd HH:mm'); formattedDateTime = dateFormatter.format(inputDateTime); } else { // 超出今年,显示【年-月-日 时:分】 final DateFormat fullDateFormatter = DateFormat('yyyy-MM-dd HH:mm'); formattedDateTime = fullDateFormatter.format(inputDateTime); } return formattedDateTime; } }