calorimeter/lib/perdate_widget.dart
2024-09-05 16:36:35 +02:00

127 lines
4.9 KiB
Dart

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<PerDateWidget> createState() => _PerDateWidgetState();
}
class _PerDateWidgetState extends State<PerDateWidget> {
late FoodStorage storage;
late Future<List<FoodEntry>> entriesFuture;
late List<FoodEntry> entries = [];
@override
void initState() {
super.initState();
storage = FoodStorage.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 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<FoodEntryBloc, FoodEntryState>(
builder: (context, state) {
return Scaffold(
appBar: AppBar(
title: Text(formattedDate),
actions: [
BlocBuilder<ThemeDataBloc, ThemeState>(
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<ThemeDataBloc>()
.add(ThemeToggleEvent());
},
);
}),
],
),
body: ListView.builder(
itemCount: state.foodEntries.length + 1,
itemBuilder:
(BuildContext itemBuilderContext, int listIndex) {
if (listIndex == state.foodEntries.length) {
return Column(
children: [
EnterFoodWidget(
onAdd: (context, entry) {
context
.read<FoodEntryBloc>()
.add(FoodEntryEvent(entry: entry));
},
),
const SizedBox(height: 75),
],
);
}
var entryIndex = listIndex;
return Column(
children: [
FoodEntryWidget(
entry: state.foodEntries[entryIndex],
onDelete: (callbackContext, id) {
callbackContext
.read<FoodEntryBloc>()
.add(FoodDeletionEvent(
entryID: id,
));
},
),
if (listIndex != state.foodEntries.length - 1)
const Divider(),
],
);
},
),
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
color: Theme.of(context).colorScheme.secondary,
child: SumWidget(foodEntries: state.foodEntries)),
drawer: const AppDrawer(),
floatingActionButton:
CalendarFloatingButton(date: widget.date),
floatingActionButtonLocation:
FloatingActionButtonLocation.endDocked);
}),
);
}
});
}
}