added popup on successfull scan

main
Julian Gegner 2023-02-20 19:32:23 +01:00
parent 74946d005c
commit 5393590687
7 changed files with 65 additions and 21 deletions

View File

@ -1,4 +0,0 @@
{
"group": "Gruppe 2",
"key": "value"
}

View File

@ -41,7 +41,10 @@ class MyHomePageState extends State<MyHomePage> {
: showDialog(
context: context,
builder: (BuildContext context) {
return const MissingConfigPopup();
return const MissingConfigPopup(
title: 'Fehlende Konfiguration',
text: 'Bitte QR Code Scannen!',
);
});
});
}

View File

@ -1,30 +1,71 @@
import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:smoke_cess_app/models/settings.dart';
import 'package:smoke_cess_app/service/json_service.dart';
import 'package:smoke_cess_app/service/settings_service.dart';
class ScannerPage extends StatelessWidget {
import '../widgets/missing_config_popup.dart';
class ScannerPage extends StatefulWidget {
const ScannerPage({super.key});
@override
State<StatefulWidget> createState() => ScannerPageState();
}
class ScannerPageState extends State<ScannerPage> {
bool scanning = false;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child: MobileScanner(fit: BoxFit.contain,onDetect: (capture) {
scanning
? Expanded(
child: MobileScanner(
fit: BoxFit.contain,
controller: MobileScannerController(
detectionTimeoutMs: 2000,
),
onDetect: (capture) {
final List<Barcode> barcodes = capture.barcodes;
for (final barcode in barcodes) {
debugPrint('Barcode found! ${barcode.rawValue}');
} },
if (barcode.rawValue != null) {
String qrText = barcode.rawValue!;
Map<String, dynamic> json = stringToJSON(qrText);
Settings settings = Settings.fromJson(json);
saveSettings(settings);
setState(() {
scanning = false;
showDialog(
context: context,
builder: (BuildContext context) {
return MissingConfigPopup(
title: 'Konfiguration erfolgreich',
text: 'Du gehörst zu Gruppe ${settings.group}',
);
});
});
}
}
},
))
,
: ElevatedButton(
style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)),
onPressed: () {
setState(() => scanning = true);
},
child: const Text('Scan QR Code'),
),
const SizedBox(height: 30),
ElevatedButton(
style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)),
onPressed: () {
loadSettings();
loadSettingsFromLocalJSON();
},
child: const Text('Read JSON'),
)

View File

@ -10,3 +10,5 @@ Future<Map<String, dynamic>> loadLocalConfigJSON() async {
List<String>? jsonPropertyAsList(dynamic property) =>
property != null ? List.from(property) : null;
Map<String, dynamic> stringToJSON(String jsonString) => jsonDecode(jsonString);

View File

@ -45,7 +45,7 @@ Future<List<String>?> _getStringListSetting(String settingKey) =>
SharedPreferences.getInstance()
.then((pref) => pref.getStringList(settingKey));
Future<void> loadSettings() async {
Future<void> loadSettingsFromLocalJSON() async {
Map<String, dynamic> configJSON = await loadLocalConfigJSON();
Settings settings = Settings.fromJson(configJSON);
saveSettings(settings);

View File

@ -1,17 +1,20 @@
import 'package:flutter/material.dart';
class MissingConfigPopup extends StatelessWidget {
const MissingConfigPopup({super.key});
final String title;
final String text;
const MissingConfigPopup(
{super.key, required this.title, required this.text});
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Fehlende Konfiguration'),
title: Text(title),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: const <Widget>[
Text("Bitte QR Code Scannen"),
children: <Widget>[
Text(text),
],
),
);

View File

@ -64,7 +64,6 @@ flutter:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
- config.json
- group1.json
- group3.json