diff --git a/lib/ui/widgets/third_screen/contacts_page.dart b/lib/ui/widgets/third_screen/contacts_page.dart index 89facacb5ed1824f73dc4847922a90ff3db778c4..62a8c57001ce10edabcd9e211d2c0f1495b9dd8b 100644 --- a/lib/ui/widgets/third_screen/contacts_page.dart +++ b/lib/ui/widgets/third_screen/contacts_page.dart @@ -8,7 +8,6 @@ import 'package:share_plus/share_plus.dart'; import '../../../cubit/bottom_nav_cubit.dart'; import '../../../data/models/contact.dart'; import '../../../data/models/contact_cubit.dart'; -import '../../../data/models/contact_state.dart'; import '../../../data/models/payment_cubit.dart'; import '../../contacts_cache.dart'; import '../../ui_helpers.dart'; @@ -24,13 +23,11 @@ class ContactsPage extends StatefulWidget { class _ContactsPageState extends State<ContactsPage> { final TextEditingController _searchController = TextEditingController(); - late ContactsCubit _contactsCubit; @override void initState() { super.initState(); - _contactsCubit = context.read<ContactsCubit>(); - _contactsCubit.resetFilter(); + context.read<ContactsCubit>().resetFilter(); } @override @@ -41,124 +38,125 @@ class _ContactsPageState extends State<ContactsPage> { @override Widget build(BuildContext context) { - return BlocBuilder<ContactsCubit, ContactsState>( - builder: (BuildContext context, ContactsState state) { - return Padding( - padding: const EdgeInsets.symmetric(horizontal: 16), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: <Widget>[ - TextField( - controller: _searchController, - decoration: InputDecoration( - hintText: tr('search_contacts'), - border: const OutlineInputBorder(), - ), - onChanged: (String query) { - _contactsCubit.filterContacts(query); - }, + final ContactsCubit cubit = context.watch<ContactsCubit>(); + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: <Widget>[ + TextField( + controller: _searchController, + decoration: InputDecoration( + hintText: tr('search_contacts'), + border: const OutlineInputBorder(), ), - const SizedBox(height: 20), - if (state.filteredContacts.isEmpty) - const NoElements(text: 'no_contacts') - else - Expanded( - child: ListView.builder( - itemCount: state.filteredContacts.length, - itemBuilder: (BuildContext context, int index) { - final Contact contact = state.filteredContacts[index]; - return Slidable( - // Specify a key if the Slidable is dismissible. - key: ValueKey<int>(index), + onChanged: (String query) { + context.read<ContactsCubit>().filterContacts(query); + }, + ), + const SizedBox(height: 20), + if (cubit.state.filteredContacts.isEmpty) + const NoElements(text: 'no_contacts') + else + Expanded( + child: ListView.builder( + itemCount: cubit.state.filteredContacts.length, + itemBuilder: (BuildContext context, int index) { + final Contact contact = cubit.state.filteredContacts[index]; + return Slidable( + // Specify a key if the Slidable is dismissible. + key: ValueKey<int>(index), - // The start action pane is the one at the left or the top side. - startActionPane: ActionPane( - // A motion is a widget used to control how the pane animates. - motion: const ScrollMotion(), + // The start action pane is the one at the left or the top side. + startActionPane: ActionPane( + // A motion is a widget used to control how the pane animates. + motion: const ScrollMotion(), - // All actions are defined in the children parameter. - children: <SlidableAction>[ - // A SlidableAction can have an icon and/or a label. - SlidableAction( - onPressed: (BuildContext c) { - _contactsCubit.removeContact(contact); - }, - backgroundColor: const Color(0xFFFE4A49), - foregroundColor: Colors.white, - icon: Icons.delete, - label: tr('delete_contact'), - ), - if (showShare()) - SlidableAction( - onPressed: (BuildContext c) => - Share.share(contact.pubKey), - backgroundColor: - Theme.of(context).secondaryHeaderColor, - foregroundColor: Theme.of(context).primaryColor, - icon: Icons.share, - label: tr('share_this_key'), - ), - ], - ), - // The end action pane is the one at the right or the bottom side. - endActionPane: ActionPane( - motion: const ScrollMotion(), - dismissible: DismissiblePane(onDismissed: () { - onSent(context, contact); - }), - children: <SlidableAction>[ + // All actions are defined in the children parameter. + children: <SlidableAction>[ + // A SlidableAction can have an icon and/or a label. + SlidableAction( + onPressed: (BuildContext c) { + context + .read<ContactsCubit>() + .removeContact(contact); + }, + backgroundColor: const Color(0xFFFE4A49), + foregroundColor: Colors.white, + icon: Icons.delete, + label: tr('delete_contact'), + ), + if (showShare()) SlidableAction( - onPressed: (BuildContext c) { - FlutterClipboard.copy(contact.pubKey).then( - (dynamic value) => ScaffoldMessenger.of( - context) - .showSnackBar(SnackBar( - content: Text(tr( - 'some_key_copied_to_clipboard'))))); - }, + onPressed: (BuildContext c) => + Share.share(contact.pubKey), backgroundColor: - Theme.of(context).primaryColorDark, - foregroundColor: Colors.white, - icon: Icons.copy, - label: tr('copy_contact_key'), + Theme.of(context).secondaryHeaderColor, + foregroundColor: Theme.of(context).primaryColor, + icon: Icons.share, + label: tr('share_this_key'), ), - SlidableAction( - onPressed: (BuildContext c) { - onSent(c, contact); - }, - backgroundColor: Theme.of(context).primaryColor, - foregroundColor: Colors.white, - icon: Icons.send, - label: tr('send_g1'), - ), - ], - ), - child: contactToListItem(contact, index, context, - onLongPress: () { - showDialog( - context: context, - builder: (BuildContext context) { - return ContactEditDialog( - contact: contact, - onSave: (Contact c) { - _contactsCubit.updateContact(c); - ContactsCache().saveContact(c); - }); + ], + ), + // The end action pane is the one at the right or the bottom side. + endActionPane: ActionPane( + motion: const ScrollMotion(), + dismissible: DismissiblePane(onDismissed: () { + onSent(context, contact); + }), + children: <SlidableAction>[ + SlidableAction( + onPressed: (BuildContext c) { + FlutterClipboard.copy(contact.pubKey).then( + (dynamic value) => ScaffoldMessenger.of( + context) + .showSnackBar(SnackBar( + content: Text(tr( + 'some_key_copied_to_clipboard'))))); }, - ); - }, onTap: () { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text(tr('long_press_to_edit')), - ), - ); - })); - }, - )), - const BottomWidget() - ], - )); - }); + backgroundColor: Theme.of(context).primaryColorDark, + foregroundColor: Colors.white, + icon: Icons.copy, + label: tr('copy_contact_key'), + ), + SlidableAction( + onPressed: (BuildContext c) { + onSent(c, contact); + }, + backgroundColor: Theme.of(context).primaryColor, + foregroundColor: Colors.white, + icon: Icons.send, + label: tr('send_g1'), + ), + ], + ), + child: contactToListItem(contact, index, context, + onLongPress: () { + showDialog( + context: context, + builder: (BuildContext context) { + return ContactEditDialog( + contact: contact, + onSave: (Contact c) { + context + .read<ContactsCubit>() + .updateContact(c); + ContactsCache().saveContact(c); + }); + }, + ); + }, onTap: () { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(tr('long_press_to_edit')), + ), + ); + })); + }, + )), + const BottomWidget() + ], + )); } void onSent(BuildContext c, Contact contact) {