2026-04-16 11:50:29 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2026-05-01 09:51:42 +02:00
|
|
|
import 'package:flux/features/operations/blocs/operations_cubit.dart';
|
2026-05-01 10:11:44 +02:00
|
|
|
import 'package:flux/features/operations/models/operation_model.dart';
|
|
|
|
|
import 'package:flux/features/operations/utils/operation_actions.dart';
|
2026-04-16 11:50:29 +02:00
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
|
// Importa i tuoi modelli e cubit
|
|
|
|
|
|
2026-05-01 10:11:44 +02:00
|
|
|
class OperationsScreen extends StatefulWidget {
|
|
|
|
|
const OperationsScreen({super.key});
|
2026-04-16 11:50:29 +02:00
|
|
|
|
|
|
|
|
@override
|
2026-05-01 10:11:44 +02:00
|
|
|
State<OperationsScreen> createState() => _OperationsScreenState();
|
2026-04-16 11:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-01 10:11:44 +02:00
|
|
|
class _OperationsScreenState extends State<OperationsScreen> {
|
2026-04-16 11:50:29 +02:00
|
|
|
final ScrollController _scrollController = ScrollController();
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
// Agganciamo il listener per la paginazione (Scroll Infinito)
|
|
|
|
|
_scrollController.addListener(_onScroll);
|
2026-04-20 16:52:20 +02:00
|
|
|
// Carichiamo i servizi iniziali
|
2026-05-01 10:11:44 +02:00
|
|
|
context.read<OperationsCubit>().loadOperations();
|
2026-04-16 11:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _onScroll() {
|
|
|
|
|
if (_isBottom) {
|
2026-05-01 10:11:44 +02:00
|
|
|
context.read<OperationsCubit>().loadOperations();
|
2026-04-16 11:50:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool get _isBottom {
|
|
|
|
|
if (!_scrollController.hasClients) return false;
|
|
|
|
|
final maxScroll = _scrollController.position.maxScrollExtent;
|
|
|
|
|
final currentScroll = _scrollController.offset;
|
|
|
|
|
// Carica quando mancano 200px alla fine
|
|
|
|
|
return currentScroll >= (maxScroll * 0.9);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_scrollController.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Scaffold(
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
title: const Text("Gestione Servizi"),
|
|
|
|
|
elevation: 0,
|
|
|
|
|
actions: [
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: const Icon(Icons.search),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
// Qui potrai implementare una barra di ricerca
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2026-05-01 10:11:44 +02:00
|
|
|
body: BlocBuilder<OperationsCubit, OperationsState>(
|
2026-04-16 11:50:29 +02:00
|
|
|
builder: (context, state) {
|
|
|
|
|
// 1. Stato di caricamento iniziale
|
2026-05-01 10:11:44 +02:00
|
|
|
if (state.status == OperationsStatus.loading &&
|
|
|
|
|
state.allOperations.isEmpty) {
|
2026-04-16 11:50:29 +02:00
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Lista vuota
|
2026-05-01 10:11:44 +02:00
|
|
|
if (state.allOperations.isEmpty) {
|
2026-04-16 11:50:29 +02:00
|
|
|
return Center(
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
const Text("Nessuna pratica trovata."),
|
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
|
ElevatedButton(
|
2026-05-01 10:11:44 +02:00
|
|
|
onPressed: () => context
|
|
|
|
|
.read<OperationsCubit>()
|
|
|
|
|
.loadOperations(refresh: true),
|
2026-04-16 11:50:29 +02:00
|
|
|
child: const Text("Riprova"),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. La Lista (con Pull-to-refresh)
|
|
|
|
|
return RefreshIndicator(
|
|
|
|
|
onRefresh: () =>
|
2026-05-01 10:11:44 +02:00
|
|
|
context.read<OperationsCubit>().loadOperations(refresh: true),
|
2026-04-16 11:50:29 +02:00
|
|
|
child: ListView.builder(
|
|
|
|
|
controller: _scrollController,
|
|
|
|
|
padding: const EdgeInsets.only(bottom: 80), // Spazio per il FAB
|
|
|
|
|
itemCount: state.hasReachedMax
|
2026-05-01 10:11:44 +02:00
|
|
|
? state.allOperations.length
|
|
|
|
|
: state.allOperations.length + 1,
|
2026-04-16 11:50:29 +02:00
|
|
|
itemBuilder: (context, index) {
|
2026-05-01 10:11:44 +02:00
|
|
|
if (index >= state.allOperations.length) {
|
2026-04-16 11:50:29 +02:00
|
|
|
return const Center(
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: EdgeInsets.all(16.0),
|
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 10:11:44 +02:00
|
|
|
final operation = state.allOperations[index];
|
|
|
|
|
return _buildOperationCard(context, operation);
|
2026-04-16 11:50:29 +02:00
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
floatingActionButton: FloatingActionButton(
|
2026-05-01 10:11:44 +02:00
|
|
|
onPressed: () => startNewOperation(context),
|
2026-04-16 11:50:29 +02:00
|
|
|
child: const Icon(Icons.add),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 10:11:44 +02:00
|
|
|
Widget _buildOperationCard(BuildContext context, OperationModel operation) {
|
2026-04-16 11:50:29 +02:00
|
|
|
return Card(
|
|
|
|
|
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
|
|
|
|
elevation: 2,
|
|
|
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
|
|
|
|
child: ListTile(
|
|
|
|
|
contentPadding: const EdgeInsets.all(12),
|
|
|
|
|
title: Row(
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
|
|
|
|
child: Text(
|
2026-05-01 09:51:42 +02:00
|
|
|
operation.customerDisplayName ?? "Cliente sconosciuto",
|
2026-04-16 11:50:29 +02:00
|
|
|
style: const TextStyle(
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-05-01 09:51:42 +02:00
|
|
|
if (operation.isBozza)
|
2026-04-16 11:50:29 +02:00
|
|
|
const Chip(
|
|
|
|
|
label: Text(
|
|
|
|
|
"BOZZA",
|
|
|
|
|
style: TextStyle(fontSize: 10, color: Colors.white),
|
|
|
|
|
),
|
|
|
|
|
backgroundColor: Colors.orange,
|
|
|
|
|
visualDensity: VisualDensity.compact,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
subtitle: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
const SizedBox(height: 4),
|
|
|
|
|
Text(
|
2026-05-01 09:51:42 +02:00
|
|
|
"Pratica: ${operation.number} • ${operation.createdAt?.day}/${operation.createdAt?.month}/${operation.createdAt?.year}",
|
2026-04-16 11:50:29 +02:00
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
// I nostri mini-chip per i servizi attivati
|
|
|
|
|
Wrap(
|
|
|
|
|
spacing: 6,
|
|
|
|
|
children: [
|
2026-05-01 09:51:42 +02:00
|
|
|
if (operation.al > 0 || operation.mnp > 0)
|
2026-04-16 11:50:29 +02:00
|
|
|
_miniBadge("📞 Tel", Colors.blue),
|
2026-05-01 10:11:44 +02:00
|
|
|
if (operation.energyOperations.isNotEmpty)
|
2026-04-16 11:50:29 +02:00
|
|
|
_miniBadge("⚡ Energy", Colors.green),
|
2026-05-01 10:11:44 +02:00
|
|
|
if (operation.finOperations.isNotEmpty)
|
2026-04-16 11:50:29 +02:00
|
|
|
_miniBadge("💰 Fin", Colors.purple),
|
2026-05-01 10:11:44 +02:00
|
|
|
if (operation.entertainmentOperations.isNotEmpty)
|
2026-04-16 11:50:29 +02:00
|
|
|
_miniBadge("📺 Ent", Colors.red),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
trailing: const Icon(Icons.chevron_right),
|
2026-04-20 16:52:20 +02:00
|
|
|
onTap: () => context.pushNamed(
|
2026-05-01 09:51:42 +02:00
|
|
|
'operation-form',
|
|
|
|
|
extra: operation, // <-- LA MAGIA È QUI: Passa l'oggetto intero!
|
2026-04-20 16:52:20 +02:00
|
|
|
// Teniamo anche il parametro URL per coerenza di routing
|
2026-05-01 09:51:42 +02:00
|
|
|
queryParameters: operation.id != null
|
2026-05-01 10:11:44 +02:00
|
|
|
? {'operationId': operation.id!}
|
2026-05-01 09:51:42 +02:00
|
|
|
: {},
|
2026-04-20 16:52:20 +02:00
|
|
|
),
|
2026-04-16 11:50:29 +02:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _miniBadge(String text, Color color) {
|
|
|
|
|
return Container(
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: color.withValues(alpha: 0.1),
|
|
|
|
|
borderRadius: BorderRadius.circular(4),
|
|
|
|
|
border: Border.all(color: color.withValues(alpha: 0.5)),
|
|
|
|
|
),
|
|
|
|
|
child: Text(
|
|
|
|
|
text,
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: color,
|
|
|
|
|
fontSize: 10,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|