/* SPDX-License-Identifier: GPL-3.0-or-later */ /* Copyright (C) 2024 Marco Groß */ import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/perdate/entry_list.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class PerDateWidget extends StatefulWidget { final DateTime date; const PerDateWidget({super.key, required this.date}); @override State createState() => _PerDateWidgetState(); } class _PerDateWidgetState extends State with AutomaticKeepAliveClientMixin { @override void initState() { context .read() .add(PageBeingInitialized(forDate: widget.date)); super.initState(); } @override Widget build(BuildContext context) { super.build(context); return BlocConsumer( listener: (context, pageState) { if (pageState.appError != null) { showNewSnackbarWith(context, pageState.appError!); } }, builder: (context, pageState) { return FoodEntryList( entries: pageState.foodEntries[widget.date] ?? [], date: widget.date); }, ); } void showNewSnackbarWith(BuildContext context, GlobalAppError error) { var snackbar = ErrorSnackbar( colorScheme: Theme.of(context).colorScheme, text: error.toErrorString(context)); ScaffoldMessenger.of(context) ..removeCurrentSnackBar() ..showSnackBar(snackbar); } @override bool get wantKeepAlive => true; } 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); }