203 lines
7.3 KiB
Dart
203 lines
7.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flux/features/customers/blocs/customer_cubit.dart';
|
|
import 'package:flux/features/customers/models/customer_model.dart';
|
|
import 'package:flux/features/customers/ui/quick_customer_dialog.dart';
|
|
import 'package:flux/features/operations/blocs/operations_cubit.dart';
|
|
|
|
class CustomerSearchSheet extends StatefulWidget {
|
|
const CustomerSearchSheet({super.key});
|
|
|
|
@override
|
|
State<CustomerSearchSheet> createState() => _CustomerSearchSheetState();
|
|
}
|
|
|
|
class _CustomerSearchSheetState extends State<CustomerSearchSheet> {
|
|
final TextEditingController _searchController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
context.read<CustomerCubit>().loadCustomers();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_searchController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onSearchChanged(String query) {
|
|
context.read<CustomerCubit>().searchCustomers(query);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ConstrainedBox(
|
|
constraints: BoxConstraints(
|
|
maxHeight: MediaQuery.of(context).size.height * 0.85,
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// --- HEADER ---
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
const Text(
|
|
"Trova Cliente",
|
|
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.close),
|
|
onPressed: () => Navigator.pop(context),
|
|
tooltip: "Chiudi",
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// --- BARRA DI RICERCA ---
|
|
TextField(
|
|
controller: _searchController,
|
|
decoration: InputDecoration(
|
|
hintText: "Cerca per nome, cognome o CF...",
|
|
prefixIcon: const Icon(Icons.search),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
filled: true,
|
|
fillColor: Theme.of(
|
|
context,
|
|
).colorScheme.surfaceContainerHighest.withValues(alpha: 0.3),
|
|
suffixIcon: IconButton(
|
|
icon: const Icon(Icons.clear),
|
|
onPressed: () {
|
|
_searchController.clear();
|
|
_onSearchChanged("");
|
|
},
|
|
),
|
|
),
|
|
onChanged: _onSearchChanged,
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// --- TASTO NUOVO CLIENTE ---
|
|
SizedBox(
|
|
width: double.infinity,
|
|
child: IconButton(
|
|
icon: const Icon(Icons.person_add),
|
|
onPressed: () async {
|
|
final operationsCubit = context.read<OperationsCubit>();
|
|
// Apriamo la dialog passando la query attuale
|
|
final CustomerModel? nuovoCliente = await showDialog(
|
|
context: context,
|
|
builder: (context) => QuickCustomerDialog(
|
|
initialQuery: _searchController.text,
|
|
),
|
|
);
|
|
|
|
if (nuovoCliente != null) {
|
|
operationsCubit.updateField(
|
|
customerId: nuovoCliente.id,
|
|
customerDisplayName: nuovoCliente.name,
|
|
);
|
|
|
|
setState(() {
|
|
_searchController.clear();
|
|
});
|
|
}
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
|
|
// --- LISTA RISULTATI CON BLOC BUILDER ---
|
|
const Text(
|
|
"Risultati",
|
|
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.grey),
|
|
),
|
|
const SizedBox(height: 8),
|
|
|
|
Expanded(
|
|
// AGGANCIO AL CUBIT REALE
|
|
child: BlocBuilder<CustomerCubit, CustomerState>(
|
|
builder: (context, state) {
|
|
// 1. Stato di caricamento
|
|
if (state.status == CustomerStatus.loading) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
// 2. Nessun risultato trovato
|
|
if (state.customers.isEmpty) {
|
|
return const Center(
|
|
child: Text(
|
|
"Nessun cliente trovato.\nProva a cambiare i termini di ricerca.",
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(color: Colors.grey),
|
|
),
|
|
);
|
|
}
|
|
|
|
// 3. Mostriamo la lista vera
|
|
return ListView.separated(
|
|
itemCount: state.customers.length,
|
|
separatorBuilder: (context, index) =>
|
|
const Divider(height: 1),
|
|
itemBuilder: (context, index) {
|
|
final customer = state.customers[index];
|
|
// Assumo che il tuo CustomerModel abbia le proprietà name e surname.
|
|
// Adatta queste variabili al tuo modello reale!
|
|
final displayName = customer.name.trim();
|
|
|
|
return ListTile(
|
|
contentPadding: EdgeInsets.zero,
|
|
leading: CircleAvatar(
|
|
backgroundColor: Theme.of(
|
|
context,
|
|
).colorScheme.primaryContainer,
|
|
foregroundColor: Theme.of(
|
|
context,
|
|
).colorScheme.onPrimaryContainer,
|
|
// Mostra l'iniziale
|
|
child: Text(
|
|
displayName.isNotEmpty
|
|
? displayName[0].toUpperCase()
|
|
: "?",
|
|
),
|
|
),
|
|
title: Text(
|
|
displayName,
|
|
style: const TextStyle(fontWeight: FontWeight.w500),
|
|
),
|
|
subtitle: Text(customer.email),
|
|
trailing: const Icon(
|
|
Icons.check_circle_outline,
|
|
color: Colors.grey,
|
|
),
|
|
onTap: () {
|
|
// Salviamo l'ID e il nome formattato nel form dei servizi
|
|
context.read<OperationsCubit>().updateField(
|
|
customerId: customer.id,
|
|
customerDisplayName: displayName,
|
|
);
|
|
|
|
// Chiudiamo la modale
|
|
Navigator.pop(context);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|