customer_list_page.dart 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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(
  27. _filter.isEmpty
  28. ? customerListProvider(agente.idAgente)
  29. : customerSearchProvider((
  30. idAgente: agente.idAgente,
  31. filter: _filter,
  32. )),
  33. )
  34. : null;
  35. return Scaffold(
  36. appBar: AppBar(title: const Text('Clienti')),
  37. body: Column(
  38. children: [
  39. sw.SearchBarWidget(
  40. controller: _searchCtrl,
  41. hint: 'Cerca cliente...',
  42. onChanged: (v) => setState(() => _filter = v),
  43. onClear: () {
  44. _searchCtrl.clear();
  45. setState(() => _filter = '');
  46. },
  47. ),
  48. Expanded(
  49. child:
  50. customersAsync?.when(
  51. data: (customers) {
  52. if (customers.isEmpty)
  53. return const EmptyState(
  54. icon: Icons.people_outline,
  55. title: 'Nessun cliente',
  56. );
  57. return ListView.builder(
  58. itemCount: customers.length,
  59. itemBuilder: (_, i) => ListTile(
  60. onTap: () => context.go(
  61. '/dashboard/customers/${customers[i].idCliente}',
  62. ),
  63. leading: CircleAvatar(
  64. child: Text(
  65. customers[i].ragioneSociale?.substring(0, 2) ??
  66. '??',
  67. ),
  68. ),
  69. title: Text(customers[i].ragioneSociale ?? ''),
  70. subtitle: Text(customers[i].codCliente ?? ''),
  71. trailing: const Icon(Icons.chevron_right),
  72. ),
  73. );
  74. },
  75. loading: () =>
  76. const Center(child: CircularProgressIndicator()),
  77. error: (e, _) => Center(child: Text('Errore: $e')),
  78. ) ??
  79. const Center(child: CircularProgressIndicator()),
  80. ),
  81. ],
  82. ),
  83. );
  84. }
  85. }