Skip to content
Snippets Groups Projects
Commit b2394253 authored by vjrj's avatar vjrj
Browse files

Contacts page refactor

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