veilidchat/lib/contacts/views/contact_item_widget.dart

120 lines
4.8 KiB
Dart
Raw Normal View History

2023-08-05 23:58:13 -04:00
import 'package:flutter/foundation.dart';
2023-08-04 01:00:38 -04:00
import 'package:flutter/material.dart';
2023-08-07 00:55:57 -04:00
import 'package:flutter_animate/flutter_animate.dart';
2024-01-30 17:03:14 -05:00
import 'package:flutter_bloc/flutter_bloc.dart';
2023-08-04 01:00:38 -04:00
import 'package:flutter_slidable/flutter_slidable.dart';
2023-08-05 23:58:13 -04:00
import 'package:flutter_translate/flutter_translate.dart';
2024-01-30 17:03:14 -05:00
import '../../chat_list/chat_list.dart';
2024-01-30 19:47:22 -05:00
import '../../layout/layout.dart';
2024-01-09 20:58:27 -05:00
import '../../proto/proto.dart' as proto;
import '../../theme/theme.dart';
2024-01-30 17:03:14 -05:00
import '../contacts.dart';
2023-08-04 01:00:38 -04:00
2024-01-09 20:58:27 -05:00
class ContactItemWidget extends StatelessWidget {
2024-02-27 12:45:58 -05:00
const ContactItemWidget(
{required this.contact, required this.disabled, super.key});
2023-08-04 01:00:38 -04:00
final proto.Contact contact;
2024-02-27 12:45:58 -05:00
final bool disabled;
2023-08-04 01:00:38 -04:00
@override
// ignore: prefer_expression_function_bodies
2024-01-09 20:58:27 -05:00
Widget build(
BuildContext context,
) {
2023-08-05 23:58:13 -04:00
final theme = Theme.of(context);
2023-09-25 22:59:28 -04:00
//final textTheme = theme.textTheme;
2023-08-05 23:58:13 -04:00
final scale = theme.extension<ScaleScheme>()!;
2023-08-04 01:00:38 -04:00
2023-08-07 00:55:57 -04:00
final remoteConversationKey =
2024-02-12 09:10:07 -05:00
contact.remoteConversationRecordKey.toVeilid();
2023-08-07 00:55:57 -04:00
2024-04-07 23:16:06 -04:00
const selected =
false; // xxx: eventually when we have selectable contacts: activeContactCubit.state == remoteConversationRecordKey;
2023-08-05 23:58:13 -04:00
return Container(
2023-09-24 15:30:54 -04:00
margin: const EdgeInsets.fromLTRB(0, 4, 0, 0),
2023-08-05 23:58:13 -04:00
clipBehavior: Clip.antiAlias,
decoration: ShapeDecoration(
2024-04-07 23:16:06 -04:00
color: selected
? scale.primaryScale.activeElementBackground
: scale.primaryScale.hoverElementBackground,
2023-08-05 23:58:13 -04:00
shape: RoundedRectangleBorder(
2023-09-24 15:30:54 -04:00
borderRadius: BorderRadius.circular(8),
2023-08-05 23:58:13 -04:00
)),
child: Slidable(
key: ObjectKey(contact),
endActionPane: ActionPane(
motion: const DrawerMotion(),
children: [
SlidableAction(
2024-02-27 21:37:39 -05:00
onPressed: disabled || context.watch<ChatListCubit>().isBusy
2024-02-27 12:45:58 -05:00
? null
: (context) async {
final contactListCubit =
context.read<ContactListCubit>();
final chatListCubit = context.read<ChatListCubit>();
2024-01-30 17:03:14 -05:00
2024-02-27 12:45:58 -05:00
// Remove any chats for this contact
await chatListCubit.deleteChat(
remoteConversationRecordKey:
remoteConversationKey);
2024-01-30 17:03:14 -05:00
2024-02-27 12:45:58 -05:00
// Delete the contact itself
await contactListCubit.deleteContact(
contact: contact);
},
2023-08-05 23:58:13 -04:00
backgroundColor: scale.tertiaryScale.background,
2024-04-07 23:16:06 -04:00
foregroundColor: scale.tertiaryScale.appText,
2023-08-05 23:58:13 -04:00
icon: Icons.delete,
label: translate('button.delete'),
padding: const EdgeInsets.all(2)),
// SlidableAction(
// onPressed: (context) => (),
// backgroundColor: scale.secondaryScale.background,
// foregroundColor: scale.secondaryScale.text,
// icon: Icons.edit,
// label: 'Edit',
// ),
],
2023-08-04 01:00:38 -04:00
),
2023-08-05 23:58:13 -04:00
// The child of the Slidable is what the user sees when the
// component is not dragged.
child: ListTile(
2024-02-27 21:37:39 -05:00
onTap: disabled || context.watch<ChatListCubit>().isBusy
2024-02-27 12:45:58 -05:00
? null
: () async {
// Start a chat
final chatListCubit = context.read<ChatListCubit>();
await chatListCubit.getOrCreateChatSingleContact(
remoteConversationRecordKey: remoteConversationKey);
// Click over to chats
if (context.mounted) {
await MainPager.of(context)
?.pageController
.animateToPage(1,
duration: 250.ms, curve: Curves.easeInOut);
}
},
2023-08-05 23:58:13 -04:00
title: Text(contact.editedProfile.name),
2023-09-28 10:06:22 -04:00
subtitle: (contact.editedProfile.pronouns.isNotEmpty)
? Text(contact.editedProfile.pronouns)
2023-08-05 23:58:13 -04:00
: null,
2024-04-07 23:16:06 -04:00
iconColor: selected
? scale.primaryScale.appText
: scale.primaryScale.subtleText,
textColor: selected
? scale.primaryScale.appText
: scale.primaryScale.subtleText,
selectedColor: scale.primaryScale.appText,
2023-08-05 23:58:13 -04:00
leading: const Icon(Icons.person))));
}
2023-08-04 01:00:38 -04:00
2023-08-05 23:58:13 -04:00
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<proto.Contact>('contact', contact));
2023-08-04 01:00:38 -04:00
}
}