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,8 +38,7 @@ class _ContactsPageState extends State<ContactsPage> { ...@@ -41,8 +38,7 @@ 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(
...@@ -55,18 +51,18 @@ class _ContactsPageState extends State<ContactsPage> { ...@@ -55,18 +51,18 @@ class _ContactsPageState extends State<ContactsPage> {
border: const OutlineInputBorder(), border: const OutlineInputBorder(),
), ),
onChanged: (String query) { onChanged: (String query) {
_contactsCubit.filterContacts(query); context.read<ContactsCubit>().filterContacts(query);
}, },
), ),
const SizedBox(height: 20), const SizedBox(height: 20),
if (state.filteredContacts.isEmpty) if (cubit.state.filteredContacts.isEmpty)
const NoElements(text: 'no_contacts') const NoElements(text: 'no_contacts')
else else
Expanded( Expanded(
child: ListView.builder( child: ListView.builder(
itemCount: state.filteredContacts.length, itemCount: cubit.state.filteredContacts.length,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
final Contact contact = state.filteredContacts[index]; final Contact contact = cubit.state.filteredContacts[index];
return Slidable( return Slidable(
// Specify a key if the Slidable is dismissible. // Specify a key if the Slidable is dismissible.
key: ValueKey<int>(index), key: ValueKey<int>(index),
...@@ -81,7 +77,9 @@ class _ContactsPageState extends State<ContactsPage> { ...@@ -81,7 +77,9 @@ class _ContactsPageState extends State<ContactsPage> {
// 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>()
.removeContact(contact);
}, },
backgroundColor: const Color(0xFFFE4A49), backgroundColor: const Color(0xFFFE4A49),
foregroundColor: Colors.white, foregroundColor: Colors.white,
...@@ -116,8 +114,7 @@ class _ContactsPageState extends State<ContactsPage> { ...@@ -116,8 +114,7 @@ class _ContactsPageState extends State<ContactsPage> {
content: Text(tr( content: Text(tr(
'some_key_copied_to_clipboard'))))); 'some_key_copied_to_clipboard')))));
}, },
backgroundColor: backgroundColor: Theme.of(context).primaryColorDark,
Theme.of(context).primaryColorDark,
foregroundColor: Colors.white, foregroundColor: Colors.white,
icon: Icons.copy, icon: Icons.copy,
label: tr('copy_contact_key'), label: tr('copy_contact_key'),
...@@ -141,7 +138,9 @@ class _ContactsPageState extends State<ContactsPage> { ...@@ -141,7 +138,9 @@ class _ContactsPageState extends State<ContactsPage> {
return ContactEditDialog( return ContactEditDialog(
contact: contact, contact: contact,
onSave: (Contact c) { onSave: (Contact c) {
_contactsCubit.updateContact(c); context
.read<ContactsCubit>()
.updateContact(c);
ContactsCache().saveContact(c); ContactsCache().saveContact(c);
}); });
}, },
...@@ -158,7 +157,6 @@ class _ContactsPageState extends State<ContactsPage> { ...@@ -158,7 +157,6 @@ class _ContactsPageState extends State<ContactsPage> {
const BottomWidget() 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.
Please register or to comment