calorimeter/lib/perdate/entry_list.dart

99 lines
3.4 KiB
Dart
Raw Normal View History

2024-12-07 12:29:34 +00:00
/* SPDX-License-Identifier: GPL-3.0-or-later */
2024-12-07 12:39:11 +00:00
/* Copyright (C) 2024 Marco Groß <mgross@sw-gross.de> */
2024-09-06 16:51:24 +00:00
import 'package:calorimeter/food_entry/enter_food_widget.dart';
2024-09-06 17:00:25 +00:00
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
import 'package:calorimeter/food_entry/food_entry_widget.dart';
import 'package:calorimeter/storage/storage.dart';
import 'package:calorimeter/utils/row_with_spacers_widget.dart';
2024-09-06 11:48:56 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2024-09-06 11:48:56 +00:00
class FoodEntryList extends StatelessWidget {
final List<FoodEntryState> entries;
final DateTime date;
2024-09-06 11:48:56 +00:00
const FoodEntryList({
required this.entries,
required this.date,
2024-09-06 11:48:56 +00:00
super.key,
});
@override
Widget build(BuildContext context) {
var headerStyle = TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurface);
return Column(
children: [
if (entries.isNotEmpty)
RowWidget(
showDividers: true,
Text(AppLocalizations.of(context)!.name, style: headerStyle),
Align(
alignment: Alignment.centerRight,
child: Text(AppLocalizations.of(context)!.amountPer,
style: headerStyle),
),
Align(
alignment: Alignment.centerRight,
child: Text(AppLocalizations.of(context)!.kcalper,
style: headerStyle),
),
Align(
alignment: Alignment.centerRight,
child: Text(AppLocalizations.of(context)!.kcalSum,
style: headerStyle),
),
),
if (entries.isNotEmpty) Divider(),
Expanded(
child: ListView.separated(
itemCount: entries.length + 1,
separatorBuilder: (context, index) {
return Divider();
},
itemBuilder: (BuildContext itemBuilderContext, int listIndex) {
//last item in list is the widget to enter food
if (listIndex == entries.length) {
return EnterFoodWidget(
foodEntryLookupDatabase:
FoodStorage.getInstance().getFoodEntryLookupDatabase,
onAdd: (context, entry) {
context
.read<FoodEntryBloc>()
.add(FoodEntryEvent(entry: entry, forDate: date));
},
);
}
var entryIndex = listIndex;
return FoodEntryWidget(
key: ValueKey(entries[entryIndex].id),
entry: entries[entryIndex],
onDelete: (_, id) {
2024-09-06 11:48:56 +00:00
context
.read<FoodEntryBloc>()
.add(FoodDeletionEvent(entryID: id, forDate: date));
2024-09-06 11:48:56 +00:00
},
onChange: (_, changedEntry) {
context.read<FoodEntryBloc>().add(
FoodChangedEvent(newEntry: changedEntry, forDate: date),
);
},
onTap: (_, tappedEntry) {
context.read<FoodEntryBloc>().add(
FoodEntryTapped(entry: tappedEntry, forDate: date),
);
},
);
},
),
),
],
2024-09-06 11:48:56 +00:00
);
}
}