Files
flux/lib/features/operations/ui/operation_form_new_screen.dart
T
2026-08-15 10:20:03 +02:00

235 lines
7.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/features/operations/blocs/operation_details_list_cubit.dart';
import 'package:flux/features/operations/blocs/operation_form_cubit.dart';
import 'package:flux/features/operations/models/operation_detail_model.dart';
import 'package:flux/features/operations/models/operation_model.dart';
class OperationFormNewScreen extends StatefulWidget {
final String? operationId;
final OperationModel? operation;
const OperationFormNewScreen({super.key, this.operationId, this.operation});
@override
State<OperationFormNewScreen> createState() => _OperationFormNewScreenState();
}
class _OperationFormNewScreenState extends State<OperationFormNewScreen> {
final _formKey = GlobalKey<FormState>();
bool _isInitialized = false;
bool _isClosingIntent = false;
final _referenceController = TextEditingController();
final _notesController = TextEditingController();
@override
void initState() {
super.initState();
// 1. Lanciamo l'inizializzazione sincrona/asincrona
context.read<OperationFormCubit>().initForm(
existingOperation: widget.operation,
operationId: widget.operationId,
);
// 2. Lettura immediata dello stato (come fatto per il customer!)
final currentState = context.read<OperationFormCubit>().state;
if (currentState.status == OperationFormStatus.ready && !_isInitialized) {
_syncTextControllers(currentState.operation);
}
}
@override
void dispose() {
_referenceController.dispose();
_notesController.dispose();
super.dispose();
}
void _syncTextControllers(OperationModel model) {
if (_referenceController.text.isEmpty) {
_referenceController.text = model.reference;
}
if (_notesController.text.isEmpty) {
_notesController.text = model.note;
}
_isInitialized = true;
}
void _flushControllersToCubit() {
context.read<OperationFormCubit>().updateFields(
reference: _referenceController.text,
note: _notesController.text,
);
}
void _saveOperation({
required OperationStatus targetStatus,
required bool isClosingIntent,
}) {
if (!_formKey.currentState!.validate()) return;
_isClosingIntent = isClosingIntent;
_flushControllersToCubit();
context.read<OperationFormCubit>().updateFields(status: targetStatus);
context.read<OperationFormCubit>().saveOperation(
targetStatus: targetStatus,
);
}
Color _getStatusColor(OperationStatus status) {
switch (status) {
case OperationStatus.success:
return Colors.green;
case OperationStatus.waitingForAction:
return Colors.orange;
case OperationStatus.waitingForSupport:
return Colors.blue;
case OperationStatus.failure:
return Colors.red;
case OperationStatus.draft:
return Colors.grey;
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
List<OperationDetailModel> details = context
.watch<OperationDetailsListCubit>()
.state
.details;
return BlocConsumer<OperationFormCubit, OperationFormState>(
listener: (context, state) {
if (state.status == OperationFormStatus.ready && !_isInitialized) {
_syncTextControllers(state.operation);
}
if (state.status == OperationFormStatus.success && _isClosingIntent) {
Navigator.of(context).pop();
} else if (state.status == OperationFormStatus.failure) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Si è verificato un errore. ${state.errorMessage}'),
backgroundColor: Colors.red,
),
);
}
},
builder: (context, state) {
if (!_isInitialized &&
(widget.operationId != null || widget.operation != null) &&
state.status == OperationFormStatus.loading) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
//TODO controlla se l'utente non ha ancora inserito dati rilevanti e nel caso cancella l'operazione e torna all'home page
Navigator.of(context).pop();
},
),
title: Text(
'Addetto selezionato: ${state.operation.staffDisplayName}',
),
actions: [
Padding(
padding: const EdgeInsets.only(right: 10),
child: Chip(
label: Text(
state.operation.status.displayName,
style: const TextStyle(color: Colors.white, fontSize: 12),
),
backgroundColor: _getStatusColor(state.operation.status),
),
),
],
),
body: Form(
key: _formKey,
child: FocusTraversalGroup(
policy: WidgetOrderTraversalPolicy(),
child: LayoutBuilder(
builder: (context, constraints) {
final isUltraWide = constraints.maxWidth > 1400;
final isDesktop = constraints.maxWidth > 900;
return SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Center(
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: isUltraWide
? 1600
: isDesktop
? 1200
: 800,
),
child: _buildResponsiveLayout(
isUltraWide,
isDesktop,
state,
),
),
),
);
},
),
),
),
bottomNavigationBar: SafeArea(
child: Container(
padding: const EdgeInsets.all(16.0),
decoration: BoxDecoration(
color: theme.scaffoldBackgroundColor,
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.05),
blurRadius: 10,
offset: const Offset(0, -3),
),
],
),
child: Row(
children: [
Expanded(
flex: 1,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: _getStatusColor(
state.operation.status,
),
foregroundColor: Colors.white,
),
onPressed: () => _saveOperation(
targetStatus: state.operation.status,
isClosingIntent: true,
),
child: state.status == OperationFormStatus.saving
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
color: Colors.white,
strokeWidth: 2,
),
)
: Text('Salva e chiudi'),
),
),
],
),
),
),
);
},
);
}
}