calorimeter/lib/utils/sum_widget.dart
Marco a7a7f44050 Make scanned widgets appear in the list instead of putting the info into
the EnterFoodWidget.

1. Make scanned foods appear in the list of foods
2. Remove Controller for entering food

This commit removes the EnterFoodController that was used to put a
scanned food into the EnterFoodWidget.
This is now unnecessary because scanning a product will be distributed
via the FoodBLoC.
2024-09-24 14:41:42 +02:00

57 lines
1.7 KiB
Dart

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';
class SumWidget extends StatelessWidget {
final List<FoodEntryState> foodEntries;
const SumWidget({required this.foodEntries, super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<SettingsDataBloc, SettingsState>(
builder: (context, state) {
var sum = 0.0;
for (var entry in foodEntries) {
sum += entry.kcalPerMass / 100 * entry.mass;
}
var diff = state.kcalLimit - sum;
var diffLimit = state.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(
'kcal heute: ${sum.ceil().toString()}/${state.kcalLimit.ceil()}',
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(color: newTextColor),
),
);
},
);
}
}