style Snackbar. still need to find a solution for async gaps in onPressed() for ScanButton

This commit is contained in:
Marco 2024-09-08 15:07:39 +02:00
parent df6e5dd8cb
commit 54157f6dec
4 changed files with 46 additions and 30 deletions

View File

@ -1,4 +1,4 @@
import 'package:calorimeter/perdate/perdate_widget.dart';
import 'package:calorimeter/utils/enter_food_controller.dart';
import 'package:calorimeter/storage/storage.dart';
import 'package:flutter/material.dart';
import 'package:calorimeter/food_entry/food_entry_bloc.dart';

View File

@ -22,7 +22,7 @@ class FoodFactLookupClient {
if (response.statusCode != HttpStatus.ok) {
return FoodFactResponse(
food: null, status: FoodFactResponseStatus.foodNotFound);
food: null, status: FoodFactResponseStatus.barcodeNotFound);
}
asString = await response.transform(utf8.decoder).join();
@ -57,7 +57,7 @@ class FoodFactModel {
enum FoodFactResponseStatus {
foodFactServerNotReachable,
foodNotFound,
barcodeNotFound,
ok,
}

View File

@ -1,7 +1,6 @@
import 'dart:developer';
import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:calorimeter/food_scan/food_fact_lookup.dart';
import 'package:calorimeter/utils/enter_food_controller.dart';
import 'package:calorimeter/utils/scan_food_floating_button.dart';
import 'package:calorimeter/utils/app_drawer.dart';
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
@ -31,7 +30,6 @@ class _PerDateWidgetState extends State<PerDateWidget> {
@override
void initState() {
log("PerDateWidgetState's initState()");
storage = FoodStorage.getInstance();
entriesFuture = storage.getEntriesForDate(widget.date);
entriesFuture.then((val) {
@ -42,7 +40,6 @@ class _PerDateWidgetState extends State<PerDateWidget> {
@override
Widget build(BuildContext context) {
log("PerDateWidgetState's build()");
return FutureBuilder(
future: entriesFuture,
builder: (context, snapshot) {
@ -80,15 +77,16 @@ class _PerDateWidgetState extends State<PerDateWidget> {
var client = FoodFactLookupClient();
var scanResult = await BarcodeScanner.scan();
if (!context.mounted) return;
if (scanResult.type == ResultType.Cancelled) {
return;
}
if (!context.mounted) return;
if (scanResult.type == ResultType.Error) {
var snackbar = const SnackBar(
content: Text("Error scanning barcode"));
ScaffoldMessenger.of(context)
.showSnackBar(snackbar);
showNewSnackbarWith(context,
"Fehler beim Scannen des Barcodes.");
}
var response = await client
.retrieveFoodInfo(scanResult.rawContent);
@ -96,21 +94,18 @@ class _PerDateWidgetState extends State<PerDateWidget> {
if (!context.mounted) return;
if (response.status ==
FoodFactResponseStatus.foodNotFound) {
var snackbar = const SnackBar(
content: Text("Food not found"));
ScaffoldMessenger.of(context)
.showSnackBar(snackbar);
FoodFactResponseStatus.barcodeNotFound) {
showNewSnackbarWith(context,
"Barcode konnte nicht gefunden werden.");
return;
}
if (response.status ==
FoodFactResponseStatus
.foodFactServerNotReachable) {
var snackbar = const SnackBar(
content:
Text("FoodFact server not reachable"));
ScaffoldMessenger.of(context)
.showSnackBar(snackbar);
showNewSnackbarWith(context,
"OpenFoodFacts-Server konnte nicht erreicht werden.");
return;
}
context.read<EnterFoodController>().set(
@ -139,15 +134,24 @@ class _PerDateWidgetState extends State<PerDateWidget> {
);
});
}
}
class EnterFoodController extends ChangeNotifier {
String name = "";
String kcalPer100g = "";
void showNewSnackbarWith(BuildContext context, String text) {
var snackbar =
ErrorSnackbar(colorScheme: Theme.of(context).colorScheme, text: text);
void set(String newName, String newKcal) {
name = newName;
kcalPer100g = newKcal;
notifyListeners();
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackbar);
}
}
class ErrorSnackbar extends SnackBar {
final String text;
final ColorScheme colorScheme;
ErrorSnackbar({
required this.text,
required this.colorScheme,
super.key,
}) : super(
content: Text(text, style: TextStyle(color: colorScheme.onError)),
backgroundColor: colorScheme.error);
}

View File

@ -0,0 +1,12 @@
import 'package:flutter/material.dart';
class EnterFoodController extends ChangeNotifier {
String name = "";
String kcalPer100g = "";
void set(String newName, String newKcal) {
name = newName;
kcalPer100g = newKcal;
notifyListeners();
}
}