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-03-06 15:54:13 +01:00
|
|
|
import 'package:smoke_cess_app/widgets/buttons/text_icon_button.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
|
|
|
|
2023-03-06 14:25:38 +01:00
|
|
|
class MyScanner extends StatelessWidget {
|
2023-02-27 17:00:28 +01:00
|
|
|
const MyScanner({super.key});
|
|
|
|
|
|
|
|
@override
|
2023-03-06 14:25:38 +01:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
SettingsProvider settingsProvider = context.watch<SettingsProvider>();
|
2023-02-27 17:00:28 +01:00
|
|
|
|
2023-03-06 18:39:36 +01:00
|
|
|
void handleSuccess(String? rawValue) {
|
|
|
|
print('in Success: $rawValue');
|
2023-03-06 14:25:38 +01:00
|
|
|
String qrText = rawValue!;
|
|
|
|
Map<String, dynamic> json = stringToJSON(qrText);
|
|
|
|
Settings settings = Settings.fromJson(json);
|
|
|
|
saveSettings(settings);
|
|
|
|
settingsProvider.initSettings();
|
|
|
|
NotificationService().setAllNotifications();
|
|
|
|
settingsProvider.scanning = false;
|
2023-02-27 17:00:28 +01:00
|
|
|
|
2023-02-27 20:30:24 +01:00
|
|
|
AwesomeDialog(
|
|
|
|
context: context,
|
|
|
|
dialogType: DialogType.success,
|
|
|
|
title: 'Geschafft',
|
|
|
|
desc: 'Der Code wurde erfolgreich gescannt!',
|
|
|
|
).show();
|
2023-03-06 14:25:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleError() {
|
|
|
|
settingsProvider.scanning = false;
|
2023-02-27 17:00:28 +01:00
|
|
|
|
2023-03-03 18:34:24 +01:00
|
|
|
AwesomeDialog(
|
|
|
|
context: context,
|
|
|
|
dialogType: DialogType.error,
|
|
|
|
title: 'Fehler',
|
|
|
|
desc: 'Der QR-Code war fehlerhaft!',
|
|
|
|
).show();
|
2023-03-06 14:25:38 +01:00
|
|
|
}
|
2023-03-03 18:34:24 +01:00
|
|
|
|
2023-03-06 14:25:38 +01:00
|
|
|
void onDetect(capture) {
|
|
|
|
try {
|
|
|
|
final List<Barcode> barcodes = capture.barcodes;
|
|
|
|
for (final barcode in barcodes) {
|
|
|
|
if (barcode.rawValue != null) {
|
2023-03-06 18:39:36 +01:00
|
|
|
print(barcode.rawValue);
|
|
|
|
return handleSuccess(barcode.rawValue);
|
2023-03-06 14:25:38 +01:00
|
|
|
}
|
2023-03-03 18:34:24 +01:00
|
|
|
}
|
2023-03-06 14:25:38 +01:00
|
|
|
} catch (e) {
|
2023-03-06 18:39:36 +01:00
|
|
|
print(e);
|
2023-03-06 14:25:38 +01:00
|
|
|
handleError();
|
2023-02-27 17:00:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 14:25:38 +01:00
|
|
|
return settingsProvider.scanning
|
2023-02-27 17:00:28 +01:00
|
|
|
? Expanded(
|
2023-03-06 15:22:02 +01:00
|
|
|
child: Stack(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
children: [
|
|
|
|
MobileScanner(
|
2023-02-27 17:00:28 +01:00
|
|
|
fit: BoxFit.contain,
|
|
|
|
controller: MobileScannerController(
|
|
|
|
detectionTimeoutMs: 2000,
|
|
|
|
),
|
2023-03-06 15:22:02 +01:00
|
|
|
onDetect: onDetect,
|
|
|
|
),
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(20),
|
|
|
|
child: Container(
|
|
|
|
height: MediaQuery.of(context).size.height / 3,
|
|
|
|
width: MediaQuery.of(context).size.width * 0.8,
|
|
|
|
color: Colors.white.withOpacity(0.4))),
|
|
|
|
],
|
|
|
|
))
|
2023-03-06 15:54:13 +01:00
|
|
|
: TextIconButton(
|
|
|
|
text: "Scan",
|
2023-03-06 14:25:38 +01:00
|
|
|
onPressed: () => settingsProvider.scanning = true,
|
2023-03-06 15:54:13 +01:00
|
|
|
iconData: Icons.qr_code_scanner_outlined);
|
2023-02-27 17:00:28 +01:00
|
|
|
}
|
|
|
|
}
|