show start & end dates in the relevant places

This commit is contained in:
Dieter Plaetinck
2025-06-27 21:02:35 +02:00
parent f18a60bfb5
commit eabe425035
3 changed files with 82 additions and 16 deletions

View File

@@ -530,7 +530,9 @@ class _PlanFormState extends State<PlanForm> {
_descriptionController.text = widget._plan.description;
_startDateController.text =
'${widget._plan.startDate.year}-${widget._plan.startDate.month.toString().padLeft(2, '0')}-${widget._plan.startDate.day.toString().padLeft(2, '0')}';
if (widget._plan.endDate != null) {
// ignore invalid enddates should the server gives us one
if (widget._plan.endDate != null &&
widget._plan.endDate!.isAfter(widget._plan.startDate)) {
_endDateController.text =
'${widget._plan.endDate!.year}-${widget._plan.endDate!.month.toString().padLeft(2, '0')}-${widget._plan.endDate!.day.toString().padLeft(2, '0')}';
}
@@ -620,8 +622,14 @@ class _PlanFormState extends State<PlanForm> {
// Open date picker
final pickedDate = await showDatePicker(
context: context,
initialDate: widget._plan.endDate,
firstDate: DateTime(2000),
// if somehow the server has an invalid end date, default to null
initialDate: (widget._plan.endDate != null &&
widget._plan.endDate!
.isAfter(widget._plan.startDate))
? widget._plan.endDate!
: null,
firstDate: widget._plan.startDate.add(
const Duration(days: 1)), // end must be after start
lastDate: DateTime(2100),
);

View File

@@ -35,9 +35,11 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
Widget build(BuildContext context) {
final nutritionalGoals = _nutritionalPlan.nutritionalGoals;
final lastWeightEntry =
Provider.of<BodyWeightProvider>(context, listen: false).getNewestEntry();
final nutritionalGoalsGperKg =
lastWeightEntry != null ? nutritionalGoals / lastWeightEntry.weight.toDouble() : null;
Provider.of<BodyWeightProvider>(context, listen: false)
.getNewestEntry();
final nutritionalGoalsGperKg = lastWeightEntry != null
? nutritionalGoals / lastWeightEntry.weight.toDouble()
: null;
return SliverList(
delegate: SliverChildListDelegate(
@@ -51,6 +53,49 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
),
),
),
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
'${AppLocalizations.of(context).start}: ',
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
),
Text(
'${_nutritionalPlan.startDate.day.toString().padLeft(2, '0')}.${_nutritionalPlan.startDate.month.toString().padLeft(2, '0')}.${_nutritionalPlan.startDate.year}',
style: Theme.of(context).textTheme.titleMedium,
),
],
),
if (_nutritionalPlan.endDate != null)
Padding(
padding: const EdgeInsets.only(top: 4.0),
child: Row(
children: [
Text(
'${AppLocalizations.of(context).endDate}: ',
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
),
Text(
'${_nutritionalPlan.endDate!.day.toString().padLeft(2, '0')}.${_nutritionalPlan.endDate!.month.toString().padLeft(2, '0')}.${_nutritionalPlan.endDate!.year}',
style: Theme.of(context).textTheme.titleMedium,
),
],
),
),
],
),
),
const SizedBox(height: 10),
..._nutritionalPlan.meals.map((meal) => MealWidget(
meal,
@@ -68,7 +113,8 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
Container(
padding: const EdgeInsets.all(15),
height: 220,
child: FlNutritionalPlanPieChartWidget(nutritionalGoals.toValues()),
child:
FlNutritionalPlanPieChartWidget(nutritionalGoals.toValues()),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),

View File

@@ -49,9 +49,15 @@ class NutritionalPlansList extends StatelessWidget {
},
title: Text(currentPlan.getLabel(context)),
subtitle: Text(
DateFormat.yMd(
Localizations.localeOf(context).languageCode,
).format(currentPlan.creationDate),
currentPlan.endDate != null
? 'from ${DateFormat.yMd(
Localizations.localeOf(context).languageCode,
).format(currentPlan.startDate)} to ${DateFormat.yMd(
Localizations.localeOf(context).languageCode,
).format(currentPlan.endDate!)}'
: 'from ${DateFormat.yMd(
Localizations.localeOf(context).languageCode,
).format(currentPlan.startDate)} (open ended)',
),
trailing: Row(mainAxisSize: MainAxisSize.min, children: [
const VerticalDivider(),
@@ -71,29 +77,35 @@ class NutritionalPlansList extends StatelessWidget {
actions: [
TextButton(
child: Text(
MaterialLocalizations.of(context).cancelButtonLabel,
MaterialLocalizations.of(context)
.cancelButtonLabel,
),
onPressed: () => Navigator.of(contextDialog).pop(),
onPressed: () =>
Navigator.of(contextDialog).pop(),
),
TextButton(
child: Text(
AppLocalizations.of(context).delete,
style: TextStyle(
color: Theme.of(context).colorScheme.error,
color:
Theme.of(context).colorScheme.error,
),
),
onPressed: () {
// Confirmed, delete the plan
_nutritionProvider.deletePlan(currentPlan.id!);
_nutritionProvider
.deletePlan(currentPlan.id!);
// Close the popup
Navigator.of(contextDialog).pop();
// and inform the user
ScaffoldMessenger.of(context).showSnackBar(
ScaffoldMessenger.of(context)
.showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context).successfullyDeleted,
AppLocalizations.of(context)
.successfullyDeleted,
textAlign: TextAlign.center,
),
),