2024-09-07 23:57:40 +00:00
|
|
|
import 'package:barcode_scan2/barcode_scan2.dart';
|
2024-09-06 23:38:03 +00:00
|
|
|
import 'package:calorimeter/utils/scan_food_floating_button.dart';
|
2024-09-06 16:51:24 +00:00
|
|
|
import 'package:calorimeter/utils/app_drawer.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
2024-09-06 16:51:24 +00:00
|
|
|
import 'package:calorimeter/perdate/entry_list.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/storage/storage.dart';
|
2024-09-06 16:51:24 +00:00
|
|
|
import 'package:calorimeter/utils/calendar_floating_button.dart';
|
|
|
|
import 'package:calorimeter/utils/rectangular_notch_shape.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/utils/sum_widget.dart';
|
|
|
|
import 'package:calorimeter/utils/theme_switcher_button.dart';
|
2024-09-06 11:48:56 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
|
|
|
class PerDateWidget extends StatefulWidget {
|
|
|
|
final DateTime date;
|
2024-10-03 21:21:13 +00:00
|
|
|
final Function(DateTime?) onDateSelected;
|
|
|
|
const PerDateWidget(
|
|
|
|
{super.key, required this.date, required this.onDateSelected});
|
2024-09-06 11:48:56 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<PerDateWidget> createState() => _PerDateWidgetState();
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:21:13 +00:00
|
|
|
class _PerDateWidgetState extends State<PerDateWidget>
|
|
|
|
with AutomaticKeepAliveClientMixin<PerDateWidget> {
|
2024-09-06 11:48:56 +00:00
|
|
|
late FoodStorage storage;
|
2024-09-09 20:41:48 +00:00
|
|
|
List<FoodEntryState> entries = [];
|
2024-09-06 11:48:56 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2024-10-07 22:58:56 +00:00
|
|
|
context
|
|
|
|
.read<FoodEntryBloc>()
|
|
|
|
.add(PageBeingInitialized(forDate: widget.date));
|
2024-09-06 11:48:56 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:21:13 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2024-09-06 11:48:56 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-10-03 21:21:13 +00:00
|
|
|
super.build(context);
|
|
|
|
|
2024-10-07 22:58:56 +00:00
|
|
|
return BlocConsumer<FoodEntryBloc, GlobalEntryState>(
|
|
|
|
listener: (context, pageState) {
|
|
|
|
if (pageState.errorString != null) {
|
|
|
|
showNewSnackbarWith(context, pageState.errorString!);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
builder: (context, globalState) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(DateFormat.yMMMMd('de').format(widget.date)),
|
|
|
|
actions: const [ThemeSwitcherButton()],
|
|
|
|
),
|
|
|
|
body: FoodEntryList(
|
|
|
|
entries: globalState.foodEntries[widget.date] ?? [],
|
|
|
|
date: widget.date),
|
|
|
|
bottomNavigationBar: BottomAppBar(
|
|
|
|
shape: const RectangularNotchShape(),
|
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
|
|
|
child: SumWidget(
|
|
|
|
foodEntries: globalState.foodEntries[widget.date] ?? [])),
|
|
|
|
drawer: const AppDrawer(),
|
|
|
|
floatingActionButton: OverflowBar(children: [
|
|
|
|
ScanFoodFloatingButton(
|
|
|
|
onPressed: () {
|
|
|
|
var result = BarcodeScanner.scan();
|
|
|
|
context.read<FoodEntryBloc>().add(
|
|
|
|
BarcodeScanned(
|
|
|
|
scanResultFuture: result,
|
|
|
|
forDate: widget.date,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
CalendarFloatingButton(
|
|
|
|
startFromDate: widget.date,
|
|
|
|
onDateSelected: (dateSelected) {
|
|
|
|
widget.onDateSelected(dateSelected);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
floatingActionButtonLocation:
|
|
|
|
FloatingActionButtonLocation.endDocked);
|
|
|
|
},
|
|
|
|
);
|
2024-09-06 11:48:56 +00:00
|
|
|
}
|
2024-09-06 22:02:01 +00:00
|
|
|
|
2024-09-08 13:07:39 +00:00
|
|
|
void showNewSnackbarWith(BuildContext context, String text) {
|
|
|
|
var snackbar =
|
|
|
|
ErrorSnackbar(colorScheme: Theme.of(context).colorScheme, text: text);
|
2024-09-06 22:02:01 +00:00
|
|
|
|
2024-09-13 14:49:38 +00:00
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
..removeCurrentSnackBar()
|
|
|
|
..showSnackBar(snackbar);
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:21:13 +00:00
|
|
|
@override
|
|
|
|
bool get wantKeepAlive => true;
|
2024-09-06 22:02:01 +00:00
|
|
|
}
|
2024-09-08 13:07:39 +00:00
|
|
|
|
|
|
|
class ErrorSnackbar extends SnackBar {
|
|
|
|
final String text;
|
|
|
|
final ColorScheme colorScheme;
|
|
|
|
ErrorSnackbar({
|
|
|
|
required this.text,
|
|
|
|
required this.colorScheme,
|
|
|
|
super.key,
|
|
|
|
}) : super(
|
|
|
|
content: Text(text, style: TextStyle(color: colorScheme.onError)),
|
|
|
|
backgroundColor: colorScheme.error);
|
|
|
|
}
|