Files
eatvio_app/lib/features/chat/presentation/widgets/chat_entry_widget.dart
2024-10-18 15:52:21 +03:00

120 lines
3.8 KiB
Dart

import 'package:eatvio_app/core/utils/date_time_utils.dart';
import 'package:eatvio_app/features/chat/models/chat_room_preview_model.dart';
import 'package:eatvio_app/theme/custom_text_style.dart';
import 'package:eatvio_app/theme/theme_helper.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
class ChatEntryWidget extends StatefulWidget {
final ChatRoom chatRoom;
ChatEntryWidget({
required this.chatRoom,
super.key,
});
@override
State<ChatEntryWidget> createState() => _ChatEntryWidgetState();
}
class _ChatEntryWidgetState extends State<ChatEntryWidget> {
LastMessageClass? lastMessage;
@override
void initState() {
super.initState();
if (widget.chatRoom.lastMessage is Map<String, dynamic>) {
lastMessage = LastMessageClass.fromJson(widget.chatRoom.lastMessage);
}
}
@override
Widget build(BuildContext context) {
return InkWell(
borderRadius: BorderRadius.circular(16),
onTap: () => GoRouter.of(context).pushNamed(
'chatScreen',
extra: widget.chatRoom.id.toString(),
),
child: Ink(
padding: EdgeInsets.all(16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Color(0xFFF6FAFF),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
widget.chatRoom.users.toChatUser.profilePhotoPath == null
? buildAvatarByUsername(widget.chatRoom.users.toChatUser.name)
: CircleAvatar(
radius: 24,
backgroundImage: NetworkImage(
widget.chatRoom.users.toChatUser.profilePhotoUrl,
),
backgroundColor: Colors.white,
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
widget.chatRoom.users.toChatUser.name.length <= 20
? widget.chatRoom.users.toChatUser.name
: widget.chatRoom.users.toChatUser.name.substring(0, 20) + '...',
style: theme.textTheme.titleMedium,
),
Spacer(),
if (lastMessage != null)
Padding(
padding: EdgeInsets.only(left: 8),
child: Text(
lastMessage!.updatedAt.format('dd MMM', 'ru'),
style: theme.textTheme.bodyMedium,
),
),
],
),
SizedBox(height: 8),
if (lastMessage != null)
Text(
lastMessage!.data.toString().trim(),
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: CustomTextStyles.bodyMediumBluegray800,
),
],
),
),
],
),
),
);
}
buildAvatarByUsername(String username) {
username = username.trim();
return Container(
width: 48,
height: 48,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Color(0xFFEBF4FF),
shape: BoxShape.circle,
),
child: Text(
username.split(' ').first[0] + username.split(' ')[1][0],
style: TextStyle(
color: Color(0xFF7F9CF5),
fontWeight: FontWeight.w400,
fontSize: 20,
),
),
);
}
}