diff --git a/lib/pages/scanner_page.dart b/lib/pages/scanner_page.dart index ebba252..14b944c 100644 --- a/lib/pages/scanner_page.dart +++ b/lib/pages/scanner_page.dart @@ -16,7 +16,7 @@ class ScannerPage extends StatelessWidget { } void loadJSON(BuildContext context) async { - var settingsModel = context.read(); + SettingsProvider settingsModel = context.read(); await loadSettingsFromLocalJSON(); settingsModel.initSettings(); NotificationService().setAllNotifications(); diff --git a/lib/providers/settings_provider.dart b/lib/providers/settings_provider.dart index f51c5e1..121ff0d 100644 --- a/lib/providers/settings_provider.dart +++ b/lib/providers/settings_provider.dart @@ -6,9 +6,16 @@ import '../models/settings.dart'; class SettingsProvider extends ChangeNotifier { Settings? _settings; bool _initialized = false; + bool _scanning = false; Settings? get settings => _settings; bool get initialized => _initialized; + bool get scanning => _scanning; + + set scanning(bool value) { + _scanning = value; + notifyListeners(); + } SettingsProvider() { initSettings(); diff --git a/lib/widgets/buttons/round_button_widget.dart b/lib/widgets/buttons/round_button_widget.dart index 14efe2d..bf31238 100644 --- a/lib/widgets/buttons/round_button_widget.dart +++ b/lib/widgets/buttons/round_button_widget.dart @@ -1,10 +1,10 @@ import 'package:flutter/material.dart'; -class RoundAddButton extends StatelessWidget { +class RoundButton extends StatelessWidget { final VoidCallback onPressed; final IconData iconData; - const RoundAddButton( + const RoundButton( {super.key, required this.onPressed, required this.iconData}); @override diff --git a/lib/widgets/scanner.dart b/lib/widgets/scanner.dart index b635d67..a4740b5 100644 --- a/lib/widgets/scanner.dart +++ b/lib/widgets/scanner.dart @@ -7,64 +7,57 @@ import 'package:smoke_cess_app/services/json_service.dart'; import 'package:smoke_cess_app/services/settings_service.dart'; import '../providers/settings_provider.dart'; import '../services/notification_service.dart'; +import 'buttons/round_button_widget.dart'; -class MyScanner extends StatefulWidget { +class MyScanner extends StatelessWidget { const MyScanner({super.key}); @override - State createState() => MyScannerState(); -} + Widget build(BuildContext context) { + SettingsProvider settingsProvider = context.watch(); -class MyScannerState extends State { - bool scanning = false; + void handleSucces(String? rawValue) { + String qrText = rawValue!; + Map 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 json = stringToJSON(qrText); - Settings settings = Settings.fromJson(json); - saveSettings(settings); - var settingsModel = context.read(); - settingsModel.initSettings(); - NotificationService().setAllNotifications(); - setState(() { - scanning = false; AwesomeDialog( context: context, dialogType: DialogType.success, title: 'Geschafft', desc: 'Der Code wurde erfolgreich gescannt!', ).show(); - }); - } + } + + void handleError() { + settingsProvider.scanning = false; - void handleError() { - setState(() { - scanning = false; AwesomeDialog( context: context, dialogType: DialogType.error, title: 'Fehler', desc: 'Der QR-Code war fehlerhaft!', ).show(); - }); - } - - void onDetect(capture) { - try { - final List barcodes = capture.barcodes; - for (final barcode in barcodes) { - if (barcode.rawValue != null) { - return handleSucces(barcode.rawValue); - } - } - } catch (e) { - handleError(); } - } - @override - Widget build(BuildContext context) { - return scanning + void onDetect(capture) { + try { + final List barcodes = capture.barcodes; + for (final barcode in barcodes) { + if (barcode.rawValue != null) { + return handleSucces(barcode.rawValue); + } + } + } catch (e) { + handleError(); + } + } + + return settingsProvider.scanning ? Expanded( child: MobileScanner( fit: BoxFit.contain, @@ -72,13 +65,9 @@ class MyScannerState extends State { detectionTimeoutMs: 2000, ), onDetect: onDetect)) - : ElevatedButton( - style: ElevatedButton.styleFrom( - textStyle: const TextStyle(fontSize: 20)), - onPressed: () { - setState(() => scanning = true); - }, - child: const Text('Scan QR Code'), + : RoundButton( + onPressed: () => settingsProvider.scanning = true, + iconData: Icons.qr_code_scanner_outlined, ); } } diff --git a/lib/widgets/view_form/view_form_page.dart b/lib/widgets/view_form/view_form_page.dart index 0b348b6..ffa0d98 100644 --- a/lib/widgets/view_form/view_form_page.dart +++ b/lib/widgets/view_form/view_form_page.dart @@ -32,7 +32,7 @@ class ViewFormPage extends StatelessWidget { if (!pageProvider.showForm) Container( margin: EdgeInsets.symmetric(vertical: height * 0.02), - child: RoundAddButton( + child: RoundButton( iconData: Icons.add_outlined, onPressed: tasksProvider.tasks[page] ?? true ? () => pageProvider.swap()