import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/perdate/entry_list.dart'; import 'package:calorimeter/storage/storage.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:provider/provider.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 { late FoodStorage storage; late Future> entriesFuture; List entries = []; @override void initState() { storage = FoodStorage.getInstance(); entriesFuture = storage.getEntriesForDate(widget.date); entriesFuture.then((val) { entries = val; }); super.initState(); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { super.build(context); return FutureBuilder( future: entriesFuture, builder: (context, snapshot) { return snapshot.connectionState != ConnectionState.done ? const Center(child: CircularProgressIndicator()) : BlocProvider( create: (context) => FoodEntryBloc( initialState: PageState(foodEntries: entries), storage: storage, forDate: widget.date, ), child: BlocConsumer( listener: (context, pageState) { if (pageState.errorString != null) { showNewSnackbarWith(context, pageState.errorString!); } }, builder: (context, pageState) { return FoodEntryList(entries: pageState.foodEntries); }, ), ); }); } void showNewSnackbarWith(BuildContext context, String text) { var snackbar = ErrorSnackbar(colorScheme: Theme.of(context).colorScheme, text: text); 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); }