b
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/widgets/flux_text_field.dart';
|
||||
import 'package:flux/features/customers/blocs/customer_form_cubit.dart';
|
||||
import 'package:flux/features/customers/models/customer_model.dart'; // Uso il tuo widget!
|
||||
|
||||
class CustomerForm extends StatefulWidget {
|
||||
final CustomerModel? customer; // Se presente, siamo in modalità "Modifica"
|
||||
final Function(CustomerModel customer) onSave;
|
||||
final CustomerModel? customer;
|
||||
final String? customerId;
|
||||
|
||||
const CustomerForm({
|
||||
super.key,
|
||||
this.customer, // Opzionale
|
||||
required this.onSave,
|
||||
});
|
||||
const CustomerForm({super.key, this.customer, this.customerId});
|
||||
|
||||
@override
|
||||
State<CustomerForm> createState() => _CustomerFormState();
|
||||
@@ -20,25 +18,19 @@ class _CustomerFormState extends State<CustomerForm> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
// Controller inizializzati con i dati del cliente (se presenti)
|
||||
late final TextEditingController _nomeController;
|
||||
late final TextEditingController _telefonoController;
|
||||
late final TextEditingController _emailController;
|
||||
late final TextEditingController _noteController;
|
||||
late bool _nonDisturbare;
|
||||
final TextEditingController _nomeController = TextEditingController();
|
||||
final TextEditingController _telefonoController = TextEditingController();
|
||||
final TextEditingController _emailController = TextEditingController();
|
||||
final TextEditingController _noteController = TextEditingController();
|
||||
bool _isInitialized = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Se widget.customer è null, i campi saranno vuoti
|
||||
_nomeController = TextEditingController(text: widget.customer?.name ?? '');
|
||||
_telefonoController = TextEditingController(
|
||||
text: widget.customer?.phoneNumber ?? '',
|
||||
context.read<CustomerFormCubit>().initForm(
|
||||
customerId: widget.customerId,
|
||||
existingCustomer: widget.customer,
|
||||
);
|
||||
_emailController = TextEditingController(
|
||||
text: widget.customer?.email ?? '',
|
||||
);
|
||||
_noteController = TextEditingController(text: widget.customer?.note ?? '');
|
||||
_nonDisturbare = widget.customer?.doNotDisturb ?? false;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -50,89 +42,131 @@ class _CustomerFormState extends State<CustomerForm> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _submit() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
// Creiamo un nuovo modello partendo da quello esistente (se c'è)
|
||||
// o creandone uno da zero, preservando l'ID in caso di modifica.
|
||||
final updatedCustomer =
|
||||
widget.customer?.copyWith(
|
||||
name: _nomeController.text.trim(),
|
||||
phoneNumber: _telefonoController.text.trim(),
|
||||
email: _emailController.text.trim(),
|
||||
note: _noteController.text.trim(),
|
||||
doNotDisturb: _nonDisturbare,
|
||||
) ??
|
||||
CustomerModel(
|
||||
// Caso nuovo cliente
|
||||
name: _nomeController.text.trim(),
|
||||
phoneNumber: _telefonoController.text.trim(),
|
||||
email: _emailController.text.trim(),
|
||||
note: _noteController.text.trim(),
|
||||
doNotDisturb: _nonDisturbare,
|
||||
companyId: '', // Verrà iniettato dal Bloc o dal chiamante
|
||||
);
|
||||
void _syncTextControllers(CustomerModel customer) {
|
||||
if (_nomeController.text.isEmpty) {
|
||||
_nomeController.text = customer.name;
|
||||
}
|
||||
if (_telefonoController.text.isEmpty) {
|
||||
_telefonoController.text = customer.phoneNumber;
|
||||
}
|
||||
if (_emailController.text.isEmpty) {
|
||||
_emailController.text = customer.email;
|
||||
}
|
||||
if (_noteController.text.isEmpty) {
|
||||
_noteController.text = customer.note;
|
||||
}
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
widget.onSave(updatedCustomer);
|
||||
void _flushControllersToCubit() {
|
||||
context.read<CustomerFormCubit>().updateFields(
|
||||
name: _nomeController.text.trim(),
|
||||
phoneNumber: _telefonoController.text.trim(),
|
||||
email: _emailController.text.trim(),
|
||||
note: _noteController.text.trim(),
|
||||
);
|
||||
}
|
||||
|
||||
void _saveCustomer() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_flushControllersToCubit();
|
||||
context.read<CustomerFormCubit>().saveCustomer();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
widget.customer == null ? 'Nuovo Cliente' : 'Modifica Cliente',
|
||||
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
return BlocConsumer<CustomerFormCubit, CustomerFormState>(
|
||||
listenWhen: (previous, current) => previous.status != current.status,
|
||||
listener: (context, state) {
|
||||
if (state.status == CustomerFormStatus.ready && !_isInitialized) {
|
||||
_syncTextControllers(state.customer);
|
||||
}
|
||||
if (state.status == CustomerFormStatus.success) {
|
||||
Navigator.of(context).pop(state.customer);
|
||||
} else if (state.status == CustomerFormStatus.failure) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(state.errorMessage ?? 'Errore sconosciuto')),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
state.customer.id == null
|
||||
? 'Nuovo Cliente'
|
||||
: 'Modifica ${state.customer.name}',
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
FluxTextField(
|
||||
label: 'Nome Completo',
|
||||
autoFocus: true,
|
||||
icon: Icons.person_outline,
|
||||
controller: _nomeController,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Telefono',
|
||||
icon: Icons.phone_android_outlined,
|
||||
controller: _telefonoController,
|
||||
keyboardType: TextInputType.phone,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Email',
|
||||
icon: Icons.alternate_email_outlined,
|
||||
controller: _emailController,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Note',
|
||||
icon: Icons.notes_outlined,
|
||||
controller: _noteController,
|
||||
minLines: 3,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
SwitchListTile(
|
||||
title: const Text('Non disturbare'),
|
||||
value: _nonDisturbare,
|
||||
onChanged: (v) => setState(() => _nonDisturbare = v),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 50,
|
||||
child: ElevatedButton(
|
||||
onPressed: _submit,
|
||||
child: Text(widget.customer == null ? 'SALVA' : 'AGGIORNA'),
|
||||
actions: [],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
FluxTextField(
|
||||
label: 'Nome Completo',
|
||||
autoFocus: true,
|
||||
icon: Icons.person_outline,
|
||||
controller: _nomeController,
|
||||
keyboardType: TextInputType.name,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Telefono',
|
||||
icon: Icons.phone_android_outlined,
|
||||
controller: _telefonoController,
|
||||
keyboardType: TextInputType.phone,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Email',
|
||||
icon: Icons.alternate_email_outlined,
|
||||
controller: _emailController,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FluxTextField(
|
||||
label: 'Note',
|
||||
icon: Icons.notes_outlined,
|
||||
controller: _noteController,
|
||||
minLines: 3,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
SwitchListTile(
|
||||
title: const Text('Non disturbare'),
|
||||
value: state.customer.doNotDisturb,
|
||||
onChanged: (v) => context
|
||||
.read<CustomerFormCubit>()
|
||||
.updateDoNotDisturb(v),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 50,
|
||||
child: ElevatedButton(
|
||||
onPressed: _saveCustomer,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
|
||||
child: Text(
|
||||
widget.customer == null ? 'SALVA' : 'AGGIORNA',
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@ import 'package:flux/features/customers/models/customer_model.dart';
|
||||
import 'package:flux/features/customers/ui/customer_form.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class CustomersContent extends StatefulWidget {
|
||||
const CustomersContent({super.key});
|
||||
class CustomersListScreen extends StatefulWidget {
|
||||
const CustomersListScreen({super.key});
|
||||
|
||||
@override
|
||||
State<CustomersContent> createState() => _CustomersContentState();
|
||||
State<CustomersListScreen> createState() => _CustomersListScreenState();
|
||||
}
|
||||
|
||||
class _CustomersContentState extends State<CustomersContent> {
|
||||
class _CustomersListScreenState extends State<CustomersListScreen> {
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
|
||||
@override
|
||||
@@ -111,7 +111,7 @@ class _CustomersContentState extends State<CustomersContent> {
|
||||
return _CustomerTile(
|
||||
customer: customer,
|
||||
onTap: () => context.pushNamed(
|
||||
Routes.customerForm,
|
||||
Routes.customerDetails,
|
||||
pathParameters: {'id': customer.id!},
|
||||
extra: customer,
|
||||
),
|
||||
Reference in New Issue
Block a user