Skip to content

Commit eabe425

Browse files
committed
show start & end dates in the relevant places
1 parent f18a60b commit eabe425

File tree

3 files changed

+82
-16
lines changed

3 files changed

+82
-16
lines changed

lib/widgets/nutrition/forms.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,9 @@ class _PlanFormState extends State<PlanForm> {
530530
_descriptionController.text = widget._plan.description;
531531
_startDateController.text =
532532
'${widget._plan.startDate.year}-${widget._plan.startDate.month.toString().padLeft(2, '0')}-${widget._plan.startDate.day.toString().padLeft(2, '0')}';
533-
if (widget._plan.endDate != null) {
533+
// ignore invalid enddates should the server gives us one
534+
if (widget._plan.endDate != null &&
535+
widget._plan.endDate!.isAfter(widget._plan.startDate)) {
534536
_endDateController.text =
535537
'${widget._plan.endDate!.year}-${widget._plan.endDate!.month.toString().padLeft(2, '0')}-${widget._plan.endDate!.day.toString().padLeft(2, '0')}';
536538
}
@@ -620,8 +622,14 @@ class _PlanFormState extends State<PlanForm> {
620622
// Open date picker
621623
final pickedDate = await showDatePicker(
622624
context: context,
623-
initialDate: widget._plan.endDate,
624-
firstDate: DateTime(2000),
625+
// if somehow the server has an invalid end date, default to null
626+
initialDate: (widget._plan.endDate != null &&
627+
widget._plan.endDate!
628+
.isAfter(widget._plan.startDate))
629+
? widget._plan.endDate!
630+
: null,
631+
firstDate: widget._plan.startDate.add(
632+
const Duration(days: 1)), // end must be after start
625633
lastDate: DateTime(2100),
626634
);
627635

lib/widgets/nutrition/nutritional_plan_detail.dart

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
3535
Widget build(BuildContext context) {
3636
final nutritionalGoals = _nutritionalPlan.nutritionalGoals;
3737
final lastWeightEntry =
38-
Provider.of<BodyWeightProvider>(context, listen: false).getNewestEntry();
39-
final nutritionalGoalsGperKg =
40-
lastWeightEntry != null ? nutritionalGoals / lastWeightEntry.weight.toDouble() : null;
38+
Provider.of<BodyWeightProvider>(context, listen: false)
39+
.getNewestEntry();
40+
final nutritionalGoalsGperKg = lastWeightEntry != null
41+
? nutritionalGoals / lastWeightEntry.weight.toDouble()
42+
: null;
4143

4244
return SliverList(
4345
delegate: SliverChildListDelegate(
@@ -51,6 +53,49 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
5153
),
5254
),
5355
),
56+
Padding(
57+
padding:
58+
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
59+
child: Column(
60+
crossAxisAlignment: CrossAxisAlignment.start,
61+
children: [
62+
Row(
63+
children: [
64+
Text(
65+
'${AppLocalizations.of(context).start}: ',
66+
style: Theme.of(context)
67+
.textTheme
68+
.titleMedium!
69+
.copyWith(fontWeight: FontWeight.bold),
70+
),
71+
Text(
72+
'${_nutritionalPlan.startDate.day.toString().padLeft(2, '0')}.${_nutritionalPlan.startDate.month.toString().padLeft(2, '0')}.${_nutritionalPlan.startDate.year}',
73+
style: Theme.of(context).textTheme.titleMedium,
74+
),
75+
],
76+
),
77+
if (_nutritionalPlan.endDate != null)
78+
Padding(
79+
padding: const EdgeInsets.only(top: 4.0),
80+
child: Row(
81+
children: [
82+
Text(
83+
'${AppLocalizations.of(context).endDate}: ',
84+
style: Theme.of(context)
85+
.textTheme
86+
.titleMedium!
87+
.copyWith(fontWeight: FontWeight.bold),
88+
),
89+
Text(
90+
'${_nutritionalPlan.endDate!.day.toString().padLeft(2, '0')}.${_nutritionalPlan.endDate!.month.toString().padLeft(2, '0')}.${_nutritionalPlan.endDate!.year}',
91+
style: Theme.of(context).textTheme.titleMedium,
92+
),
93+
],
94+
),
95+
),
96+
],
97+
),
98+
),
5499
const SizedBox(height: 10),
55100
..._nutritionalPlan.meals.map((meal) => MealWidget(
56101
meal,
@@ -68,7 +113,8 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
68113
Container(
69114
padding: const EdgeInsets.all(15),
70115
height: 220,
71-
child: FlNutritionalPlanPieChartWidget(nutritionalGoals.toValues()),
116+
child:
117+
FlNutritionalPlanPieChartWidget(nutritionalGoals.toValues()),
72118
),
73119
Padding(
74120
padding: const EdgeInsets.symmetric(horizontal: 10),

lib/widgets/nutrition/nutritional_plans_list.dart

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ class NutritionalPlansList extends StatelessWidget {
4949
},
5050
title: Text(currentPlan.getLabel(context)),
5151
subtitle: Text(
52-
DateFormat.yMd(
53-
Localizations.localeOf(context).languageCode,
54-
).format(currentPlan.creationDate),
52+
currentPlan.endDate != null
53+
? 'from ${DateFormat.yMd(
54+
Localizations.localeOf(context).languageCode,
55+
).format(currentPlan.startDate)} to ${DateFormat.yMd(
56+
Localizations.localeOf(context).languageCode,
57+
).format(currentPlan.endDate!)}'
58+
: 'from ${DateFormat.yMd(
59+
Localizations.localeOf(context).languageCode,
60+
).format(currentPlan.startDate)} (open ended)',
5561
),
5662
trailing: Row(mainAxisSize: MainAxisSize.min, children: [
5763
const VerticalDivider(),
@@ -71,29 +77,35 @@ class NutritionalPlansList extends StatelessWidget {
7177
actions: [
7278
TextButton(
7379
child: Text(
74-
MaterialLocalizations.of(context).cancelButtonLabel,
80+
MaterialLocalizations.of(context)
81+
.cancelButtonLabel,
7582
),
76-
onPressed: () => Navigator.of(contextDialog).pop(),
83+
onPressed: () =>
84+
Navigator.of(contextDialog).pop(),
7785
),
7886
TextButton(
7987
child: Text(
8088
AppLocalizations.of(context).delete,
8189
style: TextStyle(
82-
color: Theme.of(context).colorScheme.error,
90+
color:
91+
Theme.of(context).colorScheme.error,
8392
),
8493
),
8594
onPressed: () {
8695
// Confirmed, delete the plan
87-
_nutritionProvider.deletePlan(currentPlan.id!);
96+
_nutritionProvider
97+
.deletePlan(currentPlan.id!);
8898

8999
// Close the popup
90100
Navigator.of(contextDialog).pop();
91101

92102
// and inform the user
93-
ScaffoldMessenger.of(context).showSnackBar(
103+
ScaffoldMessenger.of(context)
104+
.showSnackBar(
94105
SnackBar(
95106
content: Text(
96-
AppLocalizations.of(context).successfullyDeleted,
107+
AppLocalizations.of(context)
108+
.successfullyDeleted,
97109
textAlign: TextAlign.center,
98110
),
99111
),

0 commit comments

Comments
 (0)