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 {
var settingsModel = context.read<SettingsProvider>();
SettingsProvider settingsModel = context.read<SettingsProvider>();
await loadSettingsFromLocalJSON();
settingsModel.initSettings();
NotificationService().setAllNotifications();

View File

@ -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();

View File

@ -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

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 '../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<StatefulWidget> createState() => MyScannerState();
}
Widget build(BuildContext context) {
SettingsProvider settingsProvider = context.watch<SettingsProvider>();
class MyScannerState extends State<MyScanner> {
bool scanning = false;
void handleSucces(String? rawValue) {
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(
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<Barcode> 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<Barcode> 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<MyScanner> {
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,
);
}
}

View File

@ -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()