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:calorimeter/storage/storage.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart';

View File

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

View File

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