84 lines
2.3 KiB
Dart
84 lines
2.3 KiB
Dart
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<StatefulWidget> createState() => MyScannerState();
|
|
}
|
|
|
|
class MyScannerState extends State<MyScanner> {
|
|
bool scanning = false;
|
|
|
|
void handleError(String error) {
|
|
setState(() {
|
|
scanning = false;
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return MissingConfigPopup(
|
|
title: 'Scanfehler',
|
|
text:
|
|
'Beim Scanen gab es folgende Meldung: $error. Bitte erneut versuchen',
|
|
);
|
|
});
|
|
});
|
|
}
|
|
|
|
void handleSucces(int group) {
|
|
setState(() {
|
|
scanning = false;
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return MissingConfigPopup(
|
|
title: 'Konfiguration erfolgreich',
|
|
text: 'Du gehörst zu Gruppe $group',
|
|
);
|
|
});
|
|
});
|
|
}
|
|
|
|
void onDetect(capture) {
|
|
try {
|
|
final List<Barcode> barcodes = capture.barcodes;
|
|
for (final barcode in barcodes) {
|
|
if (barcode.rawValue != null) {
|
|
String qrText = barcode.rawValue!;
|
|
Map<String, dynamic> json = stringToJSON(qrText);
|
|
Settings settings = Settings.fromJson(json);
|
|
saveSettings(settings);
|
|
handleSucces(settings.group);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
handleError(e.toString());
|
|
}
|
|
}
|
|
|
|
@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'),
|
|
);
|
|
}
|
|
}
|