use provider for scanner

main
Julian Gegner 2023-03-06 14:25:38 +01:00
parent 5ab6e598f9
commit d910e3780f
5 changed files with 44 additions and 48 deletions

View File

@ -16,7 +16,7 @@ class ScannerPage extends StatelessWidget {
} }
void loadJSON(BuildContext context) async { void loadJSON(BuildContext context) async {
var settingsModel = context.read<SettingsProvider>(); SettingsProvider settingsModel = context.read<SettingsProvider>();
await loadSettingsFromLocalJSON(); await loadSettingsFromLocalJSON();
settingsModel.initSettings(); settingsModel.initSettings();
NotificationService().setAllNotifications(); NotificationService().setAllNotifications();

View File

@ -6,9 +6,16 @@ import '../models/settings.dart';
class SettingsProvider extends ChangeNotifier { class SettingsProvider extends ChangeNotifier {
Settings? _settings; Settings? _settings;
bool _initialized = false; bool _initialized = false;
bool _scanning = false;
Settings? get settings => _settings; Settings? get settings => _settings;
bool get initialized => _initialized; bool get initialized => _initialized;
bool get scanning => _scanning;
set scanning(bool value) {
_scanning = value;
notifyListeners();
}
SettingsProvider() { SettingsProvider() {
initSettings(); initSettings();

View File

@ -1,10 +1,10 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class RoundAddButton extends StatelessWidget { class RoundButton extends StatelessWidget {
final VoidCallback onPressed; final VoidCallback onPressed;
final IconData iconData; final IconData iconData;
const RoundAddButton( const RoundButton(
{super.key, required this.onPressed, required this.iconData}); {super.key, required this.onPressed, required this.iconData});
@override @override

View File

@ -7,64 +7,57 @@ import 'package:smoke_cess_app/services/json_service.dart';
import 'package:smoke_cess_app/services/settings_service.dart'; import 'package:smoke_cess_app/services/settings_service.dart';
import '../providers/settings_provider.dart'; import '../providers/settings_provider.dart';
import '../services/notification_service.dart'; import '../services/notification_service.dart';
import 'buttons/round_button_widget.dart';
class MyScanner extends StatefulWidget { class MyScanner extends StatelessWidget {
const MyScanner({super.key}); const MyScanner({super.key});
@override @override
State<StatefulWidget> createState() => MyScannerState(); Widget build(BuildContext context) {
} SettingsProvider settingsProvider = context.watch<SettingsProvider>();
class MyScannerState extends State<MyScanner> { void handleSucces(String? rawValue) {
bool scanning = false; String qrText = rawValue!;
Map<String, dynamic> json = stringToJSON(qrText);
Settings settings = Settings.fromJson(json);
saveSettings(settings);
settingsProvider.initSettings();
NotificationService().setAllNotifications();
settingsProvider.scanning = false;
void handleSucces(String? rawValue) {
String qrText = rawValue!;
Map<String, dynamic> json = stringToJSON(qrText);
Settings settings = Settings.fromJson(json);
saveSettings(settings);
var settingsModel = context.read<SettingsProvider>();
settingsModel.initSettings();
NotificationService().setAllNotifications();
setState(() {
scanning = false;
AwesomeDialog( AwesomeDialog(
context: context, context: context,
dialogType: DialogType.success, dialogType: DialogType.success,
title: 'Geschafft', title: 'Geschafft',
desc: 'Der Code wurde erfolgreich gescannt!', desc: 'Der Code wurde erfolgreich gescannt!',
).show(); ).show();
}); }
}
void handleError() {
settingsProvider.scanning = false;
void handleError() {
setState(() {
scanning = false;
AwesomeDialog( AwesomeDialog(
context: context, context: context,
dialogType: DialogType.error, dialogType: DialogType.error,
title: 'Fehler', title: 'Fehler',
desc: 'Der QR-Code war fehlerhaft!', desc: 'Der QR-Code war fehlerhaft!',
).show(); ).show();
});
}
void onDetect(capture) {
try {
final List<Barcode> barcodes = capture.barcodes;
for (final barcode in barcodes) {
if (barcode.rawValue != null) {
return handleSucces(barcode.rawValue);
}
}
} catch (e) {
handleError();
} }
}
@override void onDetect(capture) {
Widget build(BuildContext context) { try {
return scanning final List<Barcode> barcodes = capture.barcodes;
for (final barcode in barcodes) {
if (barcode.rawValue != null) {
return handleSucces(barcode.rawValue);
}
}
} catch (e) {
handleError();
}
}
return settingsProvider.scanning
? Expanded( ? Expanded(
child: MobileScanner( child: MobileScanner(
fit: BoxFit.contain, fit: BoxFit.contain,
@ -72,13 +65,9 @@ class MyScannerState extends State<MyScanner> {
detectionTimeoutMs: 2000, detectionTimeoutMs: 2000,
), ),
onDetect: onDetect)) onDetect: onDetect))
: ElevatedButton( : RoundButton(
style: ElevatedButton.styleFrom( onPressed: () => settingsProvider.scanning = true,
textStyle: const TextStyle(fontSize: 20)), iconData: Icons.qr_code_scanner_outlined,
onPressed: () {
setState(() => scanning = true);
},
child: const Text('Scan QR Code'),
); );
} }
} }

View File

@ -32,7 +32,7 @@ class ViewFormPage extends StatelessWidget {
if (!pageProvider.showForm) if (!pageProvider.showForm)
Container( Container(
margin: EdgeInsets.symmetric(vertical: height * 0.02), margin: EdgeInsets.symmetric(vertical: height * 0.02),
child: RoundAddButton( child: RoundButton(
iconData: Icons.add_outlined, iconData: Icons.add_outlined,
onPressed: tasksProvider.tasks[page] ?? true onPressed: tasksProvider.tasks[page] ?? true
? () => pageProvider.swap() ? () => pageProvider.swap()