customer_list_page.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import '../../../providers/auth_provider.dart';
  5. import '../../../providers/customer_provider.dart';
  6. import '../../../database/tables/clienti.dart';
  7. import '../../widgets/search_bar.dart' as sw;
  8. import '../../widgets/empty_state.dart';
  9. class CustomerListPage extends ConsumerStatefulWidget {
  10. const CustomerListPage({super.key});
  11. @override
  12. ConsumerState<CustomerListPage> createState() => _CustomerListPageState();
  13. }
  14. class _CustomerListPageState extends ConsumerState<CustomerListPage> {
  15. String _filter = '';
  16. final _searchCtrl = TextEditingController();
  17. @override
  18. void dispose() {
  19. _searchCtrl.dispose();
  20. super.dispose();
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. final agente = ref.watch(authProvider).agente;
  25. final customersAsync = agente != null
  26. ? ref.watch(_filter.isEmpty
  27. ? customerListProvider(agente.idAgente)
  28. : customerSearchProvider((idAgente: agente.idAgente, filter: _filter)))
  29. : null;
  30. return Scaffold(
  31. appBar: AppBar(title: const Text('Clienti')),
  32. body: Column(
  33. children: [
  34. sw.SearchBarWidget(
  35. controller: _searchCtrl,
  36. hint: 'Cerca cliente...',
  37. onChanged: (v) => setState(() => _filter = v),
  38. onClear: () { _searchCtrl.clear(); setState(() => _filter = ''); },
  39. ),
  40. Expanded(
  41. child: customersAsync?.when(
  42. data: (customers) {
  43. if (customers.isEmpty) return const EmptyState(icon: Icons.people_outline, title: 'Nessun cliente');
  44. return ListView.builder(
  45. itemCount: customers.length,
  46. itemBuilder: (_, i) => ListTile(
  47. onTap: () => context.go('/dashboard/customers/${customers[i].idCliente}'),
  48. leading: CircleAvatar(child: Text(customers[i].codCliente?.substring(0, 2) ?? '??')),
  49. title: Text(customers[i].ragioneSociale ?? ''),
  50. subtitle: Text(customers[i].codCliente ?? ''),
  51. trailing: const Icon(Icons.chevron_right),
  52. ),
  53. );
  54. },
  55. loading: () => const Center(child: CircularProgressIndicator()),
  56. error: (e, _) => Center(child: Text('Errore: $e')),
  57. ) ?? const Center(child: CircularProgressIndicator()),
  58. ),
  59. ],
  60. ),
  61. );
  62. }
  63. }