feat: Enhance container management UI with new management components and improved QR code generation flow
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:qr_flutter/qr_flutter.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:em2rp/services/qr_code_service.dart';
|
||||
@@ -122,14 +123,42 @@ class QRCodeDialog<T> extends StatelessWidget {
|
||||
}
|
||||
|
||||
Future<void> _downloadQRCode(BuildContext context, String id) async {
|
||||
// Afficher le dialog de chargement
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (context) => Dialog(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const CircularProgressIndicator(
|
||||
valueColor: AlwaysStoppedAnimation<Color>(AppColors.rouge),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'Génération du QR Code...',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
// Générer l'image QR code en haute résolution
|
||||
final qrImage = await QRCodeService.generateQRCode(
|
||||
// Exécuter la génération dans un isolate séparé
|
||||
final qrImage = await compute(
|
||||
_generateQRCodeIsolate,
|
||||
id,
|
||||
size: 1024,
|
||||
useCache: false,
|
||||
);
|
||||
|
||||
// Fermer le dialog de chargement
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
|
||||
// Utiliser la bibliothèque printing pour sauvegarder l'image
|
||||
await Printing.sharePdf(
|
||||
bytes: qrImage,
|
||||
@@ -145,6 +174,11 @@ class QRCodeDialog<T> extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
// Fermer le dialog de chargement en cas d'erreur
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
@@ -156,6 +190,15 @@ class QRCodeDialog<T> extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fonction statique pour exécuter la génération QR code dans un isolate
|
||||
static Future<Uint8List> _generateQRCodeIsolate(String id) async {
|
||||
return await QRCodeService.generateQRCode(
|
||||
id,
|
||||
size: 1024,
|
||||
useCache: false,
|
||||
);
|
||||
}
|
||||
|
||||
/// Factory pour équipement
|
||||
static QRCodeDialog forEquipment(dynamic equipment) {
|
||||
return QRCodeDialog(
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:em2rp/models/equipment_model.dart';
|
||||
import 'package:em2rp/services/pdf_service.dart';
|
||||
import 'package:printing/printing.dart';
|
||||
|
||||
/// Widget réutilisable pour sélectionner le format de génération de QR codes multiples
|
||||
class QRCodeFormatSelectorDialog extends StatelessWidget {
|
||||
final List<EquipmentModel> equipmentList;
|
||||
/// Compatible avec n'importe quel type d'objet (équipements, conteneurs, etc.)
|
||||
class QRCodeFormatSelectorDialog<T> extends StatelessWidget {
|
||||
final List<T> itemList;
|
||||
final String Function(T) getId;
|
||||
final String Function(T) getTitle;
|
||||
final List<String> Function(T)? getDetails;
|
||||
final String dialogTitle;
|
||||
|
||||
const QRCodeFormatSelectorDialog({
|
||||
super.key,
|
||||
required this.equipmentList,
|
||||
required this.itemList,
|
||||
required this.getId,
|
||||
required this.getTitle,
|
||||
this.getDetails,
|
||||
required this.dialogTitle,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -29,7 +37,7 @@ class QRCodeFormatSelectorDialog extends StatelessWidget {
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Générer ${equipmentList.length} QR Codes',
|
||||
dialogTitle,
|
||||
style: const TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
@@ -49,7 +57,7 @@ class QRCodeFormatSelectorDialog extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Liste des équipements
|
||||
// Liste des items
|
||||
Expanded(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
@@ -58,19 +66,19 @@ class QRCodeFormatSelectorDialog extends StatelessWidget {
|
||||
),
|
||||
child: ListView.separated(
|
||||
shrinkWrap: true,
|
||||
itemCount: equipmentList.length,
|
||||
itemCount: itemList.length,
|
||||
separatorBuilder: (context, index) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final equipment = equipmentList[index];
|
||||
final item = itemList[index];
|
||||
return ListTile(
|
||||
dense: true,
|
||||
leading: const Icon(Icons.qr_code, size: 20),
|
||||
title: Text(
|
||||
equipment.id,
|
||||
getId(item),
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: Text(
|
||||
'${equipment.brand ?? ''} ${equipment.model ?? ''}'.trim(),
|
||||
getTitle(item),
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
);
|
||||
@@ -85,30 +93,21 @@ class QRCodeFormatSelectorDialog extends StatelessWidget {
|
||||
icon: Icons.qr_code,
|
||||
title: 'Petits QR Codes',
|
||||
subtitle: 'QR codes compacts (2x2 cm)',
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
_generatePDF(context, equipmentList, QRLabelFormat.small);
|
||||
},
|
||||
onPressed: () => _generatePDF(context, itemList, QRLabelFormat.small),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_FormatButton(
|
||||
icon: Icons.qr_code_2,
|
||||
title: 'QR Moyens',
|
||||
subtitle: 'QR codes taille moyenne (4x4 cm)',
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
_generatePDF(context, equipmentList, QRLabelFormat.medium);
|
||||
},
|
||||
onPressed: () => _generatePDF(context, itemList, QRLabelFormat.medium),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_FormatButton(
|
||||
icon: Icons.label,
|
||||
title: 'Grandes étiquettes',
|
||||
subtitle: 'QR + ID + Marque/Modèle (10x5 cm)',
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
_generatePDF(context, equipmentList, QRLabelFormat.large);
|
||||
},
|
||||
subtitle: 'QR + ID + Détails (10x5 cm)',
|
||||
onPressed: () => _generatePDF(context, itemList, QRLabelFormat.large),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -118,14 +117,24 @@ class QRCodeFormatSelectorDialog extends StatelessWidget {
|
||||
|
||||
Future<void> _generatePDF(
|
||||
BuildContext context,
|
||||
List<EquipmentModel> equipmentList,
|
||||
List<T> items,
|
||||
QRLabelFormat format,
|
||||
) async {
|
||||
// Capturer le navigator AVANT de fermer le premier dialog
|
||||
final navigator = Navigator.of(context);
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
|
||||
// Fermer le dialog de sélection
|
||||
navigator.pop();
|
||||
|
||||
// Attendre un court instant pour que le dialog se ferme complètement
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// Afficher le dialogue de chargement
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (context) => Dialog(
|
||||
builder: (dialogContext) => Dialog(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
@@ -144,7 +153,7 @@ class QRCodeFormatSelectorDialog extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Génération de ${equipmentList.length} QR code(s)',
|
||||
'Génération de ${items.length} QR code(s)',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey[600],
|
||||
@@ -157,31 +166,23 @@ class QRCodeFormatSelectorDialog extends StatelessWidget {
|
||||
);
|
||||
|
||||
try {
|
||||
// Génération du PDF avec progression
|
||||
final pdfBytes = await PDFService.generatePDF<EquipmentModel>(
|
||||
items: equipmentList,
|
||||
// Attendre que le dialog de chargement s'affiche complètement
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
|
||||
// Génération du PDF
|
||||
final pdfBytes = await PDFService.generatePDF<T>(
|
||||
items: items,
|
||||
format: format,
|
||||
getId: (eq) => eq.id,
|
||||
getTitle: (eq) => '${eq.brand ?? ''} ${eq.model ?? ''}'.trim(),
|
||||
getDetails: format == QRLabelFormat.large ? (EquipmentModel eq) {
|
||||
final details = <String>[];
|
||||
final brand = eq.brand;
|
||||
if (brand != null && brand.isNotEmpty) {
|
||||
details.add('Marque: $brand');
|
||||
}
|
||||
final model = eq.model;
|
||||
if (model != null && model.isNotEmpty) {
|
||||
details.add('Modèle: $model');
|
||||
}
|
||||
details.add('Catégorie: ${eq.category.label}');
|
||||
return details;
|
||||
} : null,
|
||||
getId: getId,
|
||||
getTitle: getTitle,
|
||||
getDetails: getDetails,
|
||||
);
|
||||
|
||||
// Fermer le dialogue de chargement
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
// Fermer le dialogue de chargement avec le navigator capturé
|
||||
navigator.pop();
|
||||
|
||||
// Petite pause pour s'assurer que le dialog est bien fermé
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// Afficher le PDF
|
||||
await Printing.layoutPdf(
|
||||
@@ -189,25 +190,22 @@ class QRCodeFormatSelectorDialog extends StatelessWidget {
|
||||
name: 'QRCodes_${DateTime.now().millisecondsSinceEpoch}.pdf',
|
||||
);
|
||||
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('PDF généré avec succès'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
}
|
||||
scaffoldMessenger.showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('PDF généré avec succès'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
// Fermer le dialogue de chargement en cas d'erreur
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
navigator.pop();
|
||||
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Erreur lors de la génération du PDF: $e')),
|
||||
);
|
||||
}
|
||||
scaffoldMessenger.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Erreur lors de la génération du PDF: $e'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user