calorimeter/lib/perdate_widget.dart

101 lines
3.6 KiB
Dart
Raw Normal View History

2024-06-09 17:06:10 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
2024-06-09 17:06:10 +00:00
import 'package:kalodings/app_drawer.dart';
import 'package:kalodings/calendar_floating_button.dart';
2024-06-09 17:06:10 +00:00
import 'package:kalodings/enter_food_widget.dart';
import 'package:kalodings/food_entry_bloc.dart';
import 'package:kalodings/food_entry_widget.dart';
import 'package:kalodings/storage/storage.dart';
2024-06-09 17:06:10 +00:00
import 'package:kalodings/sum_widget.dart';
class PerDateWidget extends StatefulWidget {
2024-06-09 17:06:10 +00:00
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;
});
}
2024-06-09 17:06:10 +00:00
@override
Widget build(BuildContext context) {
var formattedDate = DateFormat.yMMMMd('de').format(widget.date);
2024-06-09 17:06:10 +00:00
return Scaffold(
appBar: AppBar(
title: Text(formattedDate),
),
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<FoodEntryBloc, FoodEntryState>(
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<FoodEntryBloc>()
.add(FoodEntryEvent(entry: entry));
},
),
const SizedBox(height: 75),
],
);
}
return FoodEntryWidget(
entry: state.foodEntries[index],
onDelete: (callbackContext) {
callbackContext
2024-06-10 01:31:06 +00:00
.read<FoodEntryBloc>()
.add(FoodDeletionEvent(
entryID: state.foodEntries[index].id,
));
2024-06-10 01:31:06 +00:00
},
);
},
);
}),
);
}
2024-06-09 17:06:10 +00:00
},
),
floatingActionButton: CalendarFloatingButton(date: widget.date));
2024-06-09 17:06:10 +00:00
}
}