customer_list_page.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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: SafeArea(
  37. child: 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: customersAsync?.when(
  50. data: (customers) {
  51. if (customers.isEmpty) {
  52. return const EmptyState(
  53. icon: Icons.people_outline,
  54. title: 'Nessun cliente',
  55. );
  56. }
  57. return GridView.builder(
  58. padding: const EdgeInsets.all(16),
  59. gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
  60. maxCrossAxisExtent: 450,
  61. mainAxisSpacing: 16,
  62. crossAxisSpacing: 16,
  63. childAspectRatio: 1.2,
  64. ),
  65. itemCount: customers.length,
  66. itemBuilder: (_, i) =>
  67. CustomerCard(cliente: customers[i]),
  68. );
  69. },
  70. loading: () =>
  71. const Center(child: CircularProgressIndicator()),
  72. error: (e, _) => Center(child: Text('Errore: $e')),
  73. ) ??
  74. const Center(child: CircularProgressIndicator()),
  75. ),
  76. ],
  77. ),
  78. ),
  79. );
  80. }
  81. }