style Snackbar. still need to find a solution for async gaps in onPressed() for ScanButton
This commit is contained in:
parent
df6e5dd8cb
commit
54157f6dec
@ -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';
|
||||
|
@ -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,
|
||||
}
|
||||
|
||||
|
@ -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> {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
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 {
|
||||
String name = "";
|
||||
String kcalPer100g = "";
|
||||
|
||||
void set(String newName, String newKcal) {
|
||||
name = newName;
|
||||
kcalPer100g = newKcal;
|
||||
notifyListeners();
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
12
lib/utils/enter_food_controller.dart
Normal file
12
lib/utils/enter_food_controller.dart
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user