import 'package:flutter/material.dart'; import 'package:mobile_scanner/mobile_scanner.dart'; class QRCodeScannerScreen extends StatefulWidget { const QRCodeScannerScreen({Key? key}) : super(key: key); @override _QRCodeScannerScreenState createState() => _QRCodeScannerScreenState(); } class _QRCodeScannerScreenState extends State { final MobileScannerController cameraController = MobileScannerController(); bool isScanning = true; @override void dispose() { cameraController.dispose(); super.dispose(); } void _showInvalidQRDialog() { showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Invalid QR Code'), content: const Text('The scanned QR code does not contain valid vCard data.'), actions: [ TextButton( onPressed: () { Navigator.of(context).pop(); // Close dialog setState(() { isScanning = true; // Resume scanning }); }, child: const Text('Try Again'), ), ], ), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Scan Contact QR Code'), ), body: MobileScanner( controller: cameraController, // allowDuplicates: false, // or true, depending on your preference onDetect: (capture) { if (!isScanning) return; isScanning = false; // Stop multiple triggers final List barcodes = capture.barcodes; for (final barcode in barcodes) { final String? code = barcode.rawValue; // If the QR code contains 'BEGIN:VCARD', let's assume it's a valid vCard if (code != null && code.contains('BEGIN:VCARD')) { Navigator.pop(context, code); // pop back with the full vCard text return; } } // If no valid vCard was found in any of the barcodes _showInvalidQRDialog(); }, ), ); } }