diet-guard/app/lib/widgets/body/week_plan_card.dart
Krzysztof kuhy Rudnicki e761926df4
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Has been cancelled
Tests / test (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
Body tab: weight/body-fat logs, BMI, ideal weight, goal-based kcal target, workouts + Health Connect steps, week planner
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-28 09:03:03 +02:00

133 lines
3.8 KiB
Dart

/// Weekly-average planner: "I ate N on day X -- what now for the rest?"
///
/// Info only. Past days fill in from the food log; typing a number for any
/// day overrides it (a planned feast on Saturday too).
library;
import 'package:diet_guard_app/services/week_plan.dart';
import 'package:diet_guard_app/ui/theme.dart';
import 'package:diet_guard_app/widgets/body/body_section.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// The planner.
class WeekPlanCard extends StatefulWidget {
/// Creates the card.
const WeekPlanCard({
required this.defaultTarget,
required this.logged,
this.today,
super.key,
});
/// The average to aim for until the user types one (the daily budget).
final int defaultTarget;
/// `YYYY-MM-DD` -> kcal eaten, from the food log.
final Map<String, double> logged;
/// Overrides "today" for tests.
final DateTime? today;
@override
State<WeekPlanCard> createState() => _WeekPlanCardState();
}
class _WeekPlanCardState extends State<WeekPlanCard> {
final _target = TextEditingController();
final List<TextEditingController> _days = List.generate(
7,
(_) => TextEditingController(),
);
@override
void dispose() {
_target.dispose();
for (final controller in _days) {
controller.dispose();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
final target = int.tryParse(_target.text) ?? widget.defaultTarget;
final typed = <int, int>{
for (var i = 0; i < 7; i++)
i: ?int.tryParse(_days[i].text),
};
final plan = planWeek(
widget.today ?? DateTime.now(),
target,
widget.logged,
typed,
);
final scheme = Theme.of(context).colorScheme;
return BodySection(
title: 'Week planner (info only)',
children: [
TextField(
controller: _target,
decoration: InputDecoration(
labelText: 'Weekly average target (kcal/day)',
hintText: '${widget.defaultTarget}',
),
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
onChanged: (_) => setState(() {}),
),
const SizedBox(height: AppSpacing.sm),
for (var i = 0; i < 7; i++)
Row(
children: [
SizedBox(width: 48, child: Text(plan.days[i].weekday)),
Expanded(
child: Text(
'${plan.days[i].kcal} kcal ${_sourceLabel(plan.days[i])}',
style: TextStyle(
color: plan.days[i].source == 'plan' && plan.belowFloor
? scheme.error
: null,
),
),
),
SizedBox(
width: 96,
child: TextField(
controller: _days[i],
decoration: const InputDecoration(
hintText: 'ate…',
isDense: true,
),
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
onChanged: (_) => setState(() {}),
),
),
],
),
const SizedBox(height: AppSpacing.sm),
BodyNote(_summary(plan)),
],
);
}
}
String _sourceLabel(DayPlan day) => switch (day.source) {
'typed' => '(typed)',
'logged' => '(logged)',
'unlogged' => '(not logged -- type it)',
_ => '(plan)',
};
String _summary(WeekPlan plan) {
final per = plan.perRemaining;
if (per == null) {
return 'Every day is known: week average '
'${whole(plan.weekAvg)} kcal.';
}
final floor = plan.belowFloor ? ' -- below the 1200 kcal floor!' : '';
return 'Eat $per kcal on each open day to average '
'${plan.targetAvg}$floor';
}