Fix bug that left PerDate widget empty when popped to. #5

Merged
marco merged 1 commits from fix-empty-page-bug into master 2024-09-13 14:54:14 +00:00

View File

@ -41,24 +41,24 @@ class _PerDateWidgetState extends State<PerDateWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FutureBuilder( return FutureBuilder(
future: entriesFuture, future: entriesFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
return snapshot.connectionState != ConnectionState.done return snapshot.connectionState != ConnectionState.done
? const Center(child: CircularProgressIndicator()) ? const Center(child: CircularProgressIndicator())
: MultiProvider( : MultiProvider(
providers: [ providers: [
ChangeNotifierProvider( ChangeNotifierProvider(
create: (context) => EnterFoodController()), create: (context) => EnterFoodController()),
BlocProvider( BlocProvider(
create: (context) => FoodEntryBloc( create: (context) => FoodEntryBloc(
initialState: FoodEntryState(foodEntries: entries), initialState: FoodEntryState(foodEntries: entries),
storage: storage, storage: storage,
forDate: widget.date, forDate: widget.date,
), ),
) )
], ],
child: BlocBuilder<FoodEntryBloc, FoodEntryState>( child: BlocBuilder<FoodEntryBloc, FoodEntryState>(
builder: (context, state) { builder: (context, state) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: title:
@ -71,76 +71,92 @@ class _PerDateWidgetState extends State<PerDateWidget> {
color: Theme.of(context).colorScheme.secondary, color: Theme.of(context).colorScheme.secondary,
child: SumWidget(foodEntries: state.foodEntries)), child: SumWidget(foodEntries: state.foodEntries)),
drawer: const AppDrawer(), drawer: const AppDrawer(),
floatingActionButton: OverflowBar(children: [ floatingActionButton: OverflowBar(
ScanFoodFloatingButton( children: [
onPressed: () async { ScanFoodFloatingButton(onPressed: () {
var client = FoodFactLookupClient(); _onScanButtonPressed(context);
}),
var scanResult = await BarcodeScanner.scan(); const SizedBox(width: 8),
CalendarFloatingButton(
if (scanResult.type == ResultType.Cancelled) { startFromDate: widget.date,
return; onDateSelected: (dateSelected) {
} _onDateSelected(dateSelected);
},
if (!context.mounted) return; ),
],
if (scanResult.type == ResultType.Error) { ),
showNewSnackbarWith(context,
"Fehler beim Scannen des Barcodes.");
}
var response = await client
.retrieveFoodInfo(scanResult.rawContent);
if (!context.mounted) return;
if (response.status ==
FoodFactResponseStatus.barcodeNotFound) {
showNewSnackbarWith(context,
"Barcode konnte nicht gefunden werden.");
return;
}
if (response.status ==
FoodFactResponseStatus
.foodFactServerNotReachable) {
showNewSnackbarWith(context,
"OpenFoodFacts-Server konnte nicht erreicht werden.");
return;
}
context.read<EnterFoodController>().set(
response.food!.name,
response.food!.kcalPer100g.toString(),
);
},
),
const SizedBox(width: 8),
CalendarFloatingButton(
startFromDate: widget.date,
onDateSelected: (dateSelected) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return PerDateWidget(date: dateSelected);
},
),
);
},
),
]),
floatingActionButtonLocation: floatingActionButtonLocation:
FloatingActionButtonLocation.endDocked); FloatingActionButtonLocation.endDocked);
}), },
); ),
}); );
},
);
} }
void showNewSnackbarWith(BuildContext context, String text) { void showNewSnackbarWith(BuildContext context, String text) {
var snackbar = var snackbar =
ErrorSnackbar(colorScheme: Theme.of(context).colorScheme, text: text); ErrorSnackbar(colorScheme: Theme.of(context).colorScheme, text: text);
ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context)
ScaffoldMessenger.of(context).showSnackBar(snackbar); ..removeCurrentSnackBar()
..showSnackBar(snackbar);
}
void _onDateSelected(DateTime date) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return PerDateWidget(date: date);
},
),
).then((val) {
setState(
() {
entriesFuture = storage.getEntriesForDate(widget.date);
entriesFuture.then(
(val) {
entries = val;
},
);
},
);
});
}
void _onScanButtonPressed(BuildContext context) async {
var client = FoodFactLookupClient();
var scanResult = await BarcodeScanner.scan();
if (scanResult.type == ResultType.Cancelled) {
return;
}
if (!context.mounted) return;
if (scanResult.type == ResultType.Error) {
showNewSnackbarWith(context, "Fehler beim Scannen des Barcodes.");
}
var response = await client.retrieveFoodInfo(scanResult.rawContent);
if (!context.mounted) return;
if (response.status == FoodFactResponseStatus.barcodeNotFound) {
showNewSnackbarWith(context, "Barcode konnte nicht gefunden werden.");
return;
}
if (response.status == FoodFactResponseStatus.foodFactServerNotReachable) {
showNewSnackbarWith(
context, "OpenFoodFacts-Server konnte nicht erreicht werden.");
return;
}
context.read<EnterFoodController>().set(
response.food!.name,
response.food!.kcalPer100g.toString(),
);
} }
} }