| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import '../../../providers/auth_provider.dart';
- import '../../../providers/customer_provider.dart';
- import '../../widgets/search_bar.dart' as sw;
- import '../../widgets/empty_state.dart';
- import '../../widgets/customer_card.dart';
- class CustomerListPage extends ConsumerStatefulWidget {
- const CustomerListPage({super.key});
- @override
- ConsumerState<CustomerListPage> createState() => _CustomerListPageState();
- }
- class _CustomerListPageState extends ConsumerState<CustomerListPage> {
- String _filter = '';
- final _searchCtrl = TextEditingController();
- @override
- void dispose() {
- _searchCtrl.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- final agente = ref.watch(authProvider).agente;
- final customersAsync = agente != null
- ? ref.watch(
- _filter.isEmpty
- ? customerListProvider(agente.idAgente)
- : customerSearchProvider((
- idAgente: agente.idAgente,
- filter: _filter,
- )),
- )
- : null;
- return Scaffold(
- appBar: AppBar(title: const Text('Clienti')),
- body: SafeArea(
- child: Column(
- children: [
- sw.SearchBarWidget(
- controller: _searchCtrl,
- hint: 'Cerca cliente...',
- onChanged: (v) => setState(() => _filter = v),
- onClear: () {
- _searchCtrl.clear();
- setState(() => _filter = '');
- },
- ),
- Expanded(
- child: customersAsync?.when(
- data: (customers) {
- if (customers.isEmpty) {
- return const EmptyState(
- icon: Icons.people_outline,
- title: 'Nessun cliente',
- );
- }
- return GridView.builder(
- padding: const EdgeInsets.all(16),
- gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
- maxCrossAxisExtent: 450,
- mainAxisSpacing: 16,
- crossAxisSpacing: 16,
- childAspectRatio: 1.2,
- ),
- itemCount: customers.length,
- itemBuilder: (_, i) =>
- CustomerCard(cliente: customers[i]),
- );
- },
- loading: () =>
- const Center(child: CircularProgressIndicator()),
- error: (e, _) => Center(child: Text('Errore: $e')),
- ) ??
- const Center(child: CircularProgressIndicator()),
- ),
- ],
- ),
- ),
- );
- }
- }
|