calorimeter/lib/perdate_widget.dart

71 lines
2.4 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:go_router/go_router.dart';
import 'package:kalodings/app_drawer.dart';
import 'package:kalodings/enter_food_widget.dart';
import 'package:kalodings/food_entry_bloc.dart';
import 'package:kalodings/food_entry_widget.dart';
import 'package:kalodings/sum_widget.dart';
2024-06-09 21:25:18 +00:00
class PerDateWidget extends StatelessWidget {
2024-06-09 17:06:10 +00:00
final DateTime date;
const PerDateWidget(this.date, {super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2024-06-09 21:25:18 +00:00
title: Text(date.toString()),
2024-06-09 17:06:10 +00:00
),
drawer: const AppDrawer(),
body: 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 const SumWidget();
}
if (index == state.foodEntries.length + 1) {
return EnterFoodWidget(
onAdd: (context, entry) {
context
.read<FoodEntryBloc>()
2024-06-09 21:25:18 +00:00
.add(FoodEntryEvent(entry: entry, date: date));
2024-06-09 17:06:10 +00:00
},
);
}
return FoodEntryWidget(
entry: state.foodEntries[index],
onDelete: (callbackContext) {
callbackContext.read<FoodEntryBloc>().add(
FoodDeletionEvent(
2024-06-09 21:25:18 +00:00
entryID: state.foodEntries[index].id, date: date),
2024-06-09 17:06:10 +00:00
);
},
);
},
);
},
),
floatingActionButton: FloatingActionButton(
2024-06-09 21:25:18 +00:00
onPressed: () async {
var router = GoRouter.of(context);
var datePicked = await showDatePicker(
locale: const Locale('de'),
context: context,
initialDate: date,
currentDate: DateTime.now(),
firstDate:
DateTime.now().subtract(const Duration(days: 365 * 10)),
lastDate: DateTime.now(),
);
router.goNamed('perDay', extra: datePicked);
2024-06-09 17:06:10 +00:00
},
child: const Icon(Icons.today)),
);
}
}