calorimeter/lib/utils/sum_widget.dart

64 lines
2.1 KiB
Dart

/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2024 Marco Groß <mgross@sw-gross.de> */
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
import 'package:calorimeter/utils/settings_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class SumWidget extends StatelessWidget {
final DateTime date;
const SumWidget({super.key, required this.date});
@override
Widget build(BuildContext context) {
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;
var textColor = Theme.of(context).colorScheme.onSecondary;
var newTextColor = textColor;
var brightness = Theme.of(context).brightness;
switch (brightness) {
case Brightness.dark:
if (diff < 0) {
newTextColor = Colors.red[900]!;
} else if (diff < diffLimit) {
newTextColor = Colors.orange[900]!;
}
break;
case Brightness.light:
if (diff < 0) {
newTextColor = Colors.redAccent;
} else if (diff < diffLimit) {
newTextColor = Colors.orangeAccent;
}
break;
}
return Align(
alignment: Alignment.centerLeft,
child: Text(
'${AppLocalizations.of(context)!.kcalToday}: ${sum.ceil().toString()}/${settingsState.kcalLimit.ceil()}',
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(color: newTextColor),
),
);
},
);
});
}
}