import 'package:calodiary/theme_bloc.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:intl/intl.dart'; import 'package:calodiary/app_drawer.dart'; import 'package:calodiary/calendar_floating_button.dart'; import 'package:calodiary/enter_food_widget.dart'; import 'package:calodiary/food_entry_bloc.dart'; import 'package:calodiary/food_entry_widget.dart'; import 'package:calodiary/storage/storage.dart'; import 'package:calodiary/sum_widget.dart'; class PerDateWidget extends StatefulWidget { final DateTime date; const PerDateWidget({super.key, required this.date}); @override State createState() => _PerDateWidgetState(); } class _PerDateWidgetState extends State { late AppStorage storage; late Future> entriesFuture; late List entries; @override void initState() { super.initState(); storage = AppStorage.getInstance(); entriesFuture = storage.getEntriesForDate(widget.date); entriesFuture.then((val) { entries = val; }); } @override Widget build(BuildContext context) { var formattedDate = DateFormat.yMMMMd('de').format(widget.date); return Scaffold( appBar: AppBar( title: Text(formattedDate), actions: [ BlocBuilder(builder: (context, state) { var icon = const Icon(Icons.light_mode); if (state.brightness == 'light') { icon = const Icon(Icons.dark_mode); } return IconButton( icon: icon, onPressed: () { context.read().add(ThemeToggleEvent()); }, ); }), ], ), drawer: const AppDrawer(), body: FutureBuilder( future: entriesFuture, builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { return const Center(child: CircularProgressIndicator()); } else { return BlocProvider( create: (context) => FoodEntryBloc( initialState: FoodEntryState(foodEntries: entries), storage: storage, forDate: widget.date), child: BlocBuilder( builder: (context, state) { return ListView.builder( itemCount: state.foodEntries.length + 2, itemBuilder: (BuildContext itemBuilderContext, int index) { if (index == state.foodEntries.length) { return Padding( padding: const EdgeInsets.symmetric(vertical: 10), child: SumWidget(foodEntries: state.foodEntries), ); } if (index == state.foodEntries.length + 1) { return Column( children: [ EnterFoodWidget( onAdd: (context, entry) { context .read() .add(FoodEntryEvent(entry: entry)); }, ), const SizedBox(height: 75), ], ); } return FoodEntryWidget( entry: state.foodEntries[index], onDelete: (callbackContext) { callbackContext .read() .add(FoodDeletionEvent( entryID: state.foodEntries[index].id, )); }, ); }, ); }), ); } }, ), floatingActionButton: CalendarFloatingButton(date: widget.date)); } }