customer_list_page.dart 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../../../providers/auth_provider.dart';
  4. import '../../../providers/customer_provider.dart';
  5. import '../../widgets/search_bar.dart' as sw;
  6. import '../../widgets/empty_state.dart';
  7. import '../../widgets/customer_card.dart';
  8. class CustomerListPage extends ConsumerStatefulWidget {
  9. const CustomerListPage({super.key});
  10. @override
  11. ConsumerState<CustomerListPage> createState() => _CustomerListPageState();
  12. }
  13. class _CustomerListPageState extends ConsumerState<CustomerListPage> {
  14. String _filter = '';
  15. final _searchCtrl = TextEditingController();
  16. @override
  17. void dispose() {
  18. _searchCtrl.dispose();
  19. super.dispose();
  20. }
  21. @override
  22. Widget build(BuildContext context) {
  23. final agente = ref.watch(authProvider).agente;
  24. final customersAsync = agente != null
  25. ? ref.watch(
  26. _filter.isEmpty
  27. ? customerListProvider(agente.idAgente)
  28. : customerSearchProvider((
  29. idAgente: agente.idAgente,
  30. filter: _filter,
  31. )),
  32. )
  33. : null;
  34. return Scaffold(
  35. appBar: AppBar(title: const Text('Clienti')),
  36. body: Column(
  37. children: [
  38. sw.SearchBarWidget(
  39. controller: _searchCtrl,
  40. hint: 'Cerca cliente...',
  41. onChanged: (v) => setState(() => _filter = v),
  42. onClear: () {
  43. _searchCtrl.clear();
  44. setState(() => _filter = '');
  45. },
  46. ),
  47. Expanded(
  48. child:
  49. customersAsync?.when(
  50. data: (customers) {
  51. if (customers.isEmpty)
  52. return const EmptyState(
  53. icon: Icons.people_outline,
  54. title: 'Nessun cliente',
  55. );
  56. return GridView.builder(
  57. padding: const EdgeInsets.all(16),
  58. gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
  59. maxCrossAxisExtent: 470,
  60. mainAxisSpacing: 16,
  61. crossAxisSpacing: 16,
  62. childAspectRatio: 1.2,
  63. ),
  64. itemCount: customers.length,
  65. itemBuilder: (_, i) =>
  66. CustomerCard(cliente: customers[i]),
  67. );
  68. },
  69. loading: () =>
  70. const Center(child: CircularProgressIndicator()),
  71. error: (e, _) => Center(child: Text('Errore: $e')),
  72. ) ??
  73. const Center(child: CircularProgressIndicator()),
  74. ),
  75. ],
  76. ),
  77. );
  78. }
  79. }