2023-02-27 20:30:24 +01:00
|
|
|
import 'package:awesome_dialog/awesome_dialog.dart';
|
2023-02-27 17:00:28 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
2023-02-27 17:15:34 +01:00
|
|
|
import 'package:provider/provider.dart';
|
2023-02-27 17:00:28 +01:00
|
|
|
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';
|
2023-02-27 17:15:34 +01:00
|
|
|
import '../providers/settings_provider.dart';
|
|
|
|
import '../services/notification_service.dart';
|
2023-02-27 17:00:28 +01:00
|
|
|
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;
|
|
|
|
|
2023-02-27 17:09:58 +01:00
|
|
|
void handleError() {
|
2023-02-27 17:00:28 +01:00
|
|
|
setState(() {
|
|
|
|
scanning = false;
|
2023-02-27 20:30:24 +01:00
|
|
|
AwesomeDialog(
|
|
|
|
context: context,
|
|
|
|
dialogType: DialogType.error,
|
|
|
|
title: 'Upss',
|
|
|
|
desc: 'Da muss etwas schiefgelaufen sein!',
|
|
|
|
).show();
|
2023-02-27 17:00:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-27 17:09:58 +01:00
|
|
|
void handleSucces(String? rawValue) {
|
|
|
|
String qrText = rawValue!;
|
|
|
|
Map<String, dynamic> json = stringToJSON(qrText);
|
|
|
|
Settings settings = Settings.fromJson(json);
|
|
|
|
saveSettings(settings);
|
2023-02-27 17:15:34 +01:00
|
|
|
var settingsModel = context.read<SettingsProvider>();
|
|
|
|
settingsModel.initSettings();
|
|
|
|
NotificationService().setAllNotifications();
|
2023-02-27 17:00:28 +01:00
|
|
|
setState(() {
|
|
|
|
scanning = false;
|
2023-02-27 20:30:24 +01:00
|
|
|
AwesomeDialog(
|
|
|
|
context: context,
|
|
|
|
dialogType: DialogType.success,
|
|
|
|
title: 'Geschafft',
|
|
|
|
desc: 'Der Code wurde erfolgreich gescannt!',
|
|
|
|
).show();
|
2023-02-27 17:00:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDetect(capture) {
|
|
|
|
try {
|
|
|
|
final List<Barcode> barcodes = capture.barcodes;
|
|
|
|
for (final barcode in barcodes) {
|
|
|
|
if (barcode.rawValue != null) {
|
2023-02-27 17:09:58 +01:00
|
|
|
handleSucces(barcode.rawValue);
|
2023-02-27 17:00:28 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-27 17:09:58 +01:00
|
|
|
} finally {
|
|
|
|
handleError();
|
2023-02-27 17:00:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|