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-06-09 12:42:17 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
|
|
|
import 'package:calorimeter/utils/settings_bloc.dart';
|
2024-12-10 21:37:13 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-06-09 12:42:17 +00:00
|
|
|
|
2024-09-07 23:57:40 +00:00
|
|
|
class SumWidget extends StatelessWidget {
|
2024-10-18 00:44:43 +00:00
|
|
|
final DateTime date;
|
|
|
|
|
|
|
|
const SumWidget({super.key, required this.date});
|
2024-06-10 01:06:56 +00:00
|
|
|
|
2024-06-09 12:42:17 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-10-18 00:44:43 +00:00
|
|
|
return BlocBuilder<FoodEntryBloc, GlobalEntryState>(
|
|
|
|
builder: (context, entryState) {
|
|
|
|
return BlocBuilder<SettingsDataBloc, SettingsState>(
|
|
|
|
builder: (context, settingsState) {
|
|
|
|
var sum = 0.0;
|
|
|
|
for (var entry in entryState.foodEntries[date] ?? []) {
|
|
|
|
sum += entry.kcalPer100 / 100 * entry.mass;
|
|
|
|
}
|
|
|
|
var diff = settingsState.kcalLimit - sum;
|
|
|
|
var diffLimit = settingsState.kcalLimit ~/ 4;
|
2024-09-07 23:57:40 +00:00
|
|
|
|
2024-10-18 00:44:43 +00:00
|
|
|
var textColor = Theme.of(context).colorScheme.onSecondary;
|
|
|
|
var newTextColor = textColor;
|
|
|
|
var brightness = Theme.of(context).brightness;
|
2024-09-07 23:57:40 +00:00
|
|
|
|
2024-10-18 00:44:43 +00:00
|
|
|
switch (brightness) {
|
|
|
|
case Brightness.dark:
|
|
|
|
if (diff < 0) {
|
|
|
|
newTextColor = Colors.red[900]!;
|
|
|
|
} else if (diff < diffLimit) {
|
|
|
|
newTextColor = Colors.orange[900]!;
|
|
|
|
}
|
|
|
|
break;
|
2024-09-07 23:57:40 +00:00
|
|
|
|
2024-10-18 00:44:43 +00:00
|
|
|
case Brightness.light:
|
|
|
|
if (diff < 0) {
|
|
|
|
newTextColor = Colors.redAccent;
|
|
|
|
} else if (diff < diffLimit) {
|
|
|
|
newTextColor = Colors.orangeAccent;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-06-09 12:42:17 +00:00
|
|
|
|
2024-10-18 00:44:43 +00:00
|
|
|
return Align(
|
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
child: Text(
|
2024-12-10 21:37:13 +00:00
|
|
|
'${AppLocalizations.of(context)!.kcalToday}: ${sum.ceil().toString()}/${settingsState.kcalLimit.ceil()}',
|
2024-10-18 00:44:43 +00:00
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.bodyLarge!
|
|
|
|
.copyWith(color: newTextColor),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2024-06-09 12:42:17 +00:00
|
|
|
}
|
|
|
|
}
|