import 'package:flutter/material.dart'; import 'package:mobile_scanner/mobile_scanner.dart'; import 'package:smoke_cess_app/models/settings.dart'; import 'package:smoke_cess_app/services/json_service.dart'; import 'package:smoke_cess_app/services/settings_service.dart'; import '../widgets/missing_config_popup.dart'; class MyScanner extends StatefulWidget { const MyScanner({super.key}); @override State createState() => MyScannerState(); } class MyScannerState extends State { bool scanning = false; void handleError() { setState(() { scanning = false; showDialog( context: context, builder: (BuildContext context) { return const MissingConfigPopup( title: 'Scanfehler', text: 'Beim Scanen gab es wohl einen Fehler. Bitte erneut versuchen', ); }); }); } void handleSucces(String? rawValue) { String qrText = rawValue!; Map json = stringToJSON(qrText); Settings settings = Settings.fromJson(json); saveSettings(settings); setState(() { scanning = false; showDialog( context: context, builder: (BuildContext context) { return MissingConfigPopup( title: 'Konfiguration erfolgreich', text: 'Du gehörst zu Gruppe ${settings.group}', ); }); }); } void onDetect(capture) { try { final List barcodes = capture.barcodes; for (final barcode in barcodes) { if (barcode.rawValue != null) { handleSucces(barcode.rawValue); } } } finally { handleError(); } } @override Widget build(BuildContext context) { return scanning ? Expanded( child: MobileScanner( fit: BoxFit.contain, controller: MobileScannerController( detectionTimeoutMs: 2000, ), onDetect: onDetect)) : ElevatedButton( style: ElevatedButton.styleFrom( textStyle: const TextStyle(fontSize: 20)), onPressed: () { setState(() => scanning = true); }, child: const Text('Scan QR Code'), ); } }