2023-06-21 17:44:28 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import 'package:flutter_slidable/flutter_slidable.dart';
|
|
|
|
import 'package:go_router/go_router.dart';
|
2023-08-10 19:08:46 +08:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
2023-06-21 17:44:28 +08:00
|
|
|
|
|
|
|
import 'package:together_mobile/common/constants.dart';
|
2023-08-10 19:08:46 +08:00
|
|
|
import 'package:together_mobile/database/box_type.dart';
|
|
|
|
import 'package:together_mobile/models/contact_model.dart';
|
|
|
|
import 'package:together_mobile/models/init_get_it.dart';
|
|
|
|
import 'package:together_mobile/request/server.dart';
|
|
|
|
import 'package:together_mobile/utils/format_datetime.dart';
|
|
|
|
import 'badge_avatar.dart';
|
2023-06-21 17:44:28 +08:00
|
|
|
|
2023-08-10 19:08:46 +08:00
|
|
|
class ChatTile extends StatefulWidget {
|
|
|
|
const ChatTile({
|
|
|
|
super.key,
|
|
|
|
required this.contactId,
|
|
|
|
});
|
|
|
|
|
|
|
|
final String contactId;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ChatTile> createState() => _ChatTileState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChatTileState extends State<ChatTile> {
|
|
|
|
late Map<String, dynamic> _latestMsg;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
Box<MessageT> messageTBox =
|
|
|
|
Hive.box<MessageT>('message_${widget.contactId}');
|
|
|
|
int length = messageTBox.length;
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
MessageT messageT = messageTBox.getAt(length - 1)!;
|
|
|
|
_latestMsg = {
|
|
|
|
'senderId': messageT.senderId,
|
|
|
|
'type': messageT.type,
|
|
|
|
'text': messageT.text,
|
|
|
|
'attachments': messageT.attachments,
|
|
|
|
'dateTime': formatMessageDateTime(messageT.dateTime),
|
|
|
|
'isShowTime': messageT.isShowTime,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
_latestMsg = {
|
|
|
|
'senderId': '',
|
|
|
|
'type': '',
|
|
|
|
'text': '',
|
|
|
|
'attachments': [],
|
|
|
|
'dateTime': [],
|
|
|
|
'isShowTime': false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 17:44:28 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Slidable(
|
|
|
|
key: const ValueKey(0),
|
|
|
|
endActionPane: ActionPane(
|
|
|
|
motion: const BehindMotion(),
|
|
|
|
dismissible: DismissiblePane(
|
|
|
|
onDismissed: () {},
|
|
|
|
),
|
|
|
|
children: [
|
|
|
|
SlidableAction(
|
|
|
|
onPressed: (BuildContext context) {},
|
|
|
|
backgroundColor: kSecondaryColor,
|
|
|
|
foregroundColor: kContentColorDark,
|
|
|
|
icon: Icons.arrow_upward_rounded,
|
|
|
|
label: '置顶',
|
|
|
|
),
|
|
|
|
SlidableAction(
|
|
|
|
onPressed: (BuildContext context) {},
|
|
|
|
foregroundColor: kContentColorDark,
|
|
|
|
backgroundColor: kPrimaryColor,
|
2023-08-10 19:08:46 +08:00
|
|
|
icon: Icons.remove_red_eye,
|
|
|
|
label: '隐藏信息',
|
2023-06-21 17:44:28 +08:00
|
|
|
flex: 1,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
child: ListTile(
|
|
|
|
// Must have a onTap callback or Ink won't work
|
2023-08-10 19:08:46 +08:00
|
|
|
onTap: () => context.goNamed(
|
|
|
|
'Message',
|
|
|
|
queryParameters: {'contactId': widget.contactId},
|
2023-06-21 17:44:28 +08:00
|
|
|
),
|
2023-08-10 19:08:46 +08:00
|
|
|
leading: getIt
|
|
|
|
.get<ContactAccountProfile>()
|
|
|
|
.friends[widget.contactId]!
|
|
|
|
.avatar
|
|
|
|
.isEmpty
|
|
|
|
? const BadgeAvatar(
|
|
|
|
count: 99,
|
|
|
|
radius: 25,
|
|
|
|
backgroundImage: AssetImage('assets/images/user_3.png'),
|
|
|
|
)
|
|
|
|
: BadgeAvatar(
|
|
|
|
count: 99,
|
|
|
|
radius: 25,
|
|
|
|
backgroundImage: CachedNetworkImageProvider(
|
|
|
|
'$avatarsUrl/${getIt.get<ContactAccountProfile>().friends[widget.contactId]!.avatar}',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
title: Row(
|
2023-06-21 17:44:28 +08:00
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
2023-08-10 19:08:46 +08:00
|
|
|
// friend remark or nickname
|
2023-06-21 17:44:28 +08:00
|
|
|
child: Text(
|
2023-08-10 19:08:46 +08:00
|
|
|
getIt
|
|
|
|
.get<Contact>()
|
|
|
|
.friends[widget.contactId]!
|
|
|
|
.friendRemark
|
|
|
|
.isEmpty
|
|
|
|
? getIt
|
|
|
|
.get<ContactAccountProfile>()
|
|
|
|
.friends[widget.contactId]!
|
|
|
|
.nickname
|
|
|
|
: getIt
|
|
|
|
.get<Contact>()
|
|
|
|
.friends[widget.contactId]!
|
|
|
|
.friendRemark,
|
2023-06-21 17:44:28 +08:00
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
),
|
2023-08-10 19:08:46 +08:00
|
|
|
// latest msg datetime
|
|
|
|
Text(
|
|
|
|
_latestMsg['dateTime']!,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 14,
|
|
|
|
color: kUnActivatedColor,
|
|
|
|
),
|
|
|
|
),
|
2023-06-21 17:44:28 +08:00
|
|
|
],
|
|
|
|
),
|
|
|
|
subtitle: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(
|
2023-08-10 19:08:46 +08:00
|
|
|
_latestMsg['text'],
|
2023-06-21 17:44:28 +08:00
|
|
|
overflow: TextOverflow.ellipsis,
|
2023-08-10 19:08:46 +08:00
|
|
|
style: const TextStyle(
|
|
|
|
color: kUnActivatedColor,
|
|
|
|
fontSize: 15,
|
|
|
|
),
|
2023-06-21 17:44:28 +08:00
|
|
|
),
|
|
|
|
),
|
|
|
|
// Text('10:13'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|