import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:logging/logging.dart'; import 'package:provider/provider.dart'; import 'package:relatica/controls/standard_app_drawer.dart'; import 'package:relatica/utils/active_profile_selector.dart'; import '../controls/app_bottom_nav_bar.dart'; import '../controls/current_profile_button.dart'; import '../controls/padding.dart'; import '../controls/status_and_refresh_button.dart'; import '../globals.dart'; import '../models/connection.dart'; import '../routes.dart'; import '../services/connections_manager.dart'; import '../services/network_status_service.dart'; class ContactsScreen extends StatefulWidget { @override State createState() => _ContactsScreenState(); } class _ContactsScreenState extends State { static final _logger = Logger('$ContactsScreen'); var filterText = ''; @override Widget build(BuildContext context) { final nss = getIt(); final manager = context .watch>() .activeEntry .value; final allContacts = manager.getMyContacts(); final filterTextLC = filterText.toLowerCase(); final contacts = allContacts .where((c) => filterText.isEmpty || c.name.toLowerCase().contains(filterTextLC) || c.handle.toLowerCase().contains(filterTextLC)) .toList(); contacts.sort((c1, c2) => c1.name.compareTo(c2.name)); _logger.finer( () => '# Contacts: ${allContacts.length}, #filtered: ${contacts.length}', ); late Widget body; if (contacts.isEmpty) { body = const SingleChildScrollView( physics: AlwaysScrollableScrollPhysics(), child: Text('No Contacts'), ); } else { body = ListView.separated( physics: const AlwaysScrollableScrollPhysics(), itemBuilder: (context, index) { final contact = contacts[index]; return ListTile( onTap: () { context.pushNamed(ScreenPaths.userProfile, params: {'id': contact.id}); }, title: Text(contact.name), trailing: Text(contact.status.label()), ); }, separatorBuilder: (context, index) => const Divider(), itemCount: contacts.length); } final profileButton = buildCurrentProfileButton(context); return Scaffold( drawer: StandardAppDrawer(), body: SafeArea( child: RefreshIndicator( onRefresh: () async { if (nss.connectionUpdateStatus.value) { return; } await manager.updateAllContacts(); }, child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.all(8.0), child: Row( children: [ if (profileButton != null) ...[ SizedBox(width: 70.0, child: profileButton), const HorizontalPadding(), ], Expanded( child: TextField( onChanged: (value) { setState(() { filterText = value.toLowerCase(); }); }, decoration: InputDecoration( labelText: 'Filter By Name', alignLabelWithHint: true, border: OutlineInputBorder( borderSide: BorderSide( color: Theme.of(context).backgroundColor, ), borderRadius: BorderRadius.circular(5.0), ), ), ), ), const HorizontalPadding(), StatusAndRefreshButton( valueListenable: nss.connectionUpdateStatus, refreshFunction: () async => await manager.updateAllContacts(), ) ], ), ), const VerticalPadding(), Expanded( child: body, ), ], ), ), ), bottomNavigationBar: AppBottomNavBar( currentButton: NavBarButtons.contacts, ), ); } }