calorimeter/lib/perdate/perdate_widget.dart

68 lines
1.9 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 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 11:48:56 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class PerDateWidget extends StatefulWidget {
final DateTime date;
2024-10-06 23:44:19 +00:00
const PerDateWidget({super.key, required this.date});
2024-09-06 11:48:56 +00:00
@override
State<PerDateWidget> createState() => _PerDateWidgetState();
}
class _PerDateWidgetState extends State<PerDateWidget>
with AutomaticKeepAliveClientMixin<PerDateWidget> {
2024-09-06 11:48:56 +00:00
@override
void initState() {
context
.read<FoodEntryBloc>()
.add(PageBeingInitialized(forDate: widget.date));
2024-09-06 11:48:56 +00:00
super.initState();
}
@override
Widget build(BuildContext context) {
super.build(context);
return BlocConsumer<FoodEntryBloc, GlobalEntryState>(
listener: (context, pageState) {
if (pageState.errorString != null) {
showNewSnackbarWith(context, pageState.errorString!);
}
},
builder: (context, pageState) {
return FoodEntryList(
entries: pageState.foodEntries[widget.date] ?? [],
date: widget.date);
},
);
2024-09-06 11:48:56 +00:00
}
void showNewSnackbarWith(BuildContext context, String text) {
var snackbar =
ErrorSnackbar(colorScheme: Theme.of(context).colorScheme, text: text);
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(snackbar);
}
@override
bool get wantKeepAlive => true;
}
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);
}