diff --git a/lib/features/services/ui/service_form_screen/attachment_section.dart b/lib/features/services/ui/service_form_screen/attachment_section.dart index 1643421..1558277 100644 --- a/lib/features/services/ui/service_form_screen/attachment_section.dart +++ b/lib/features/services/ui/service_form_screen/attachment_section.dart @@ -247,35 +247,30 @@ class AttachmentsSection extends StatelessWidget { Future _handleGenerateQr(BuildContext context) async { final cubit = context.read(); var currentService = cubit.state.currentService; - final navigator = Navigator.of(context); - // 1. SE LA PRATICA E' NUOVA (Manca l'ID) + // 1. CATTURIAMO IL BLOC MENTRE SIAMO ANCORA NELLA PAGINA + final serviceFilesBloc = context.read(); + + // 2. SE LA PRATICA E' NUOVA (Manca l'ID) if (currentService == null || currentService.id == null) { - // Chiediamo conferma + // NIENTE BlocListener qui! Solo un semplice Dialog di conferma final bool? confirm = await showDialog( context: context, - builder: (ctx) => BlocListener( - listener: (context, state) { - if (state.status == ServiceFilesStatus.success) { - navigator.pop(); - } - }, - child: AlertDialog( - title: const Text("Salvataggio Necessario"), - content: const Text( - "Per generare il QR Code e caricare file dal telefono, la pratica deve essere prima salvata in BOZZA.\n\nVuoi salvare ora?", - ), - actions: [ - TextButton( - onPressed: () => Navigator.pop(ctx, false), - child: const Text("Annulla"), - ), - ElevatedButton( - onPressed: () => Navigator.pop(ctx, true), - child: const Text("Salva in Bozza"), - ), - ], + builder: (ctx) => AlertDialog( + title: const Text("Salvataggio Necessario"), + content: const Text( + "Per generare il QR Code e caricare file dal telefono, la pratica deve essere prima salvata in BOZZA.\n\nVuoi salvare ora?", ), + actions: [ + TextButton( + onPressed: () => Navigator.pop(ctx, false), + child: const Text("Annulla"), + ), + ElevatedButton( + onPressed: () => Navigator.pop(ctx, true), + child: const Text("Salva in Bozza"), + ), + ], ), ); @@ -287,24 +282,36 @@ class AttachmentsSection extends StatelessWidget { // Recuperiamo il servizio aggiornato con l'ID! currentService = cubit.state.currentService; - if (currentService?.id == null) { - // Se c'è stato un errore nel salvataggio, usciamo - return; - } + if (currentService?.id == null) return; } - // 2. ORA ABBIAMO L'ID SICURO -> MOSTRIAMO IL QR! + // 3. MOSTRIAMO IL QR CODE (Con il Ponte e l'Auto-Chiusura!) if (context.mounted) { - // Creiamo un nome leggibile da passare nel link final nomePratica = "Pratica ${currentService?.customerDisplayName ?? ''}" .trim(); showDialog( context: context, - builder: (context) => QrUploadDialog( - deepLinkUrl: - 'fluxapp://service/${currentService!.id}/upload?name=${Uri.encodeComponent(nomePratica)}', - title: 'Scatta per\n$nomePratica', + builder: (dialogContext) => BlocProvider.value( + // INIETTIAMO IL BLOC NEL CONTESTO DEL DIALOG ALIENO + value: serviceFilesBloc, + + // ORA METTIAMO L'AUTO-CHIUSURA SUL QR CODE! + child: BlocListener( + listener: (context, state) { + // Se arrivano file remoti e lo stato è success, chiudiamo il QR! + // (Nota: usiamo dialogContext per assicurarci di chiudere il popup giusto) + if (state.status == ServiceFilesStatus.success && + state.remoteFiles.isNotEmpty) { + Navigator.of(dialogContext).pop(); + } + }, + child: QrUploadDialog( + deepLinkUrl: + 'fluxapp://service/${currentService!.id}/upload?name=${Uri.encodeComponent(nomePratica)}', + title: 'Scatta per\n$nomePratica', + ), + ), ), ); }