together_mobile/lib/screens/chat/components/chat_tile.dart

144 lines
4.4 KiB
Dart
Raw Normal View History

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 '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,
2023-08-11 23:02:31 +08:00
required this.index,
2023-08-10 19:08:46 +08:00
required this.contactId,
2023-08-11 23:02:31 +08:00
required this.senderId,
required this.type,
required this.text,
required this.attachments,
required this.dateTime,
required this.isShowTime,
2023-08-10 19:08:46 +08:00
});
2023-08-11 23:02:31 +08:00
final int index;
final String contactId, senderId, type, text, dateTime;
final List<String> attachments;
final bool isShowTime;
2023-08-10 19:08:46 +08:00
@override
State<ChatTile> createState() => _ChatTileState();
}
class _ChatTileState extends State<ChatTile> {
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(
2023-08-11 23:02:31 +08:00
onDismissed: () {
Box<ChatSetting> chatSettingBox = Hive.box('chat_setting');
ChatSetting chatSetting = chatSettingBox.getAt(widget.index)!;
chatSetting.isOpen = false;
chatSettingBox.put(widget.contactId, chatSetting);
},
2023-06-21 17:44:28 +08:00
),
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(
2023-08-11 23:02:31 +08:00
widget.dateTime,
2023-08-10 19:08:46 +08:00
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-11 23:02:31 +08:00
widget.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
),
),
],
),
),
);
}
}