-
-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathweight.dart
More file actions
193 lines (173 loc) · 6.03 KB
/
Copy pathweight.dart
File metadata and controls
193 lines (173 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2020 - 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/workouts/weight_unit.dart';
import 'package:wger/providers/gym_log_state.dart';
import 'package:wger/providers/plate_weights.dart';
import 'package:wger/providers/routines.dart';
/// Input widget for workout weight units
///
/// Can be used with a Setting or a Log object
class WeightUnitInputWidget extends StatefulWidget {
final WeightUnit? selectedWeightUnit;
final ValueChanged<WeightUnit> onChanged;
const WeightUnitInputWidget(this.selectedWeightUnit, {super.key, required this.onChanged});
@override
_WeightUnitInputWidgetState createState() => _WeightUnitInputWidgetState();
}
class _WeightUnitInputWidgetState extends State<WeightUnitInputWidget> {
@override
Widget build(BuildContext context) {
final unitProvider = context.read<RoutinesProvider>();
WeightUnit? selectedWeightUnit = widget.selectedWeightUnit;
return DropdownButtonFormField(
initialValue: selectedWeightUnit,
decoration: InputDecoration(labelText: AppLocalizations.of(context).weightUnit),
onChanged: (WeightUnit? newValue) {
if (newValue == null) {
return;
}
setState(() {
selectedWeightUnit = newValue;
widget.onChanged(newValue);
});
},
items: unitProvider.weightUnits.map<DropdownMenuItem<WeightUnit>>((WeightUnit value) {
return DropdownMenuItem<WeightUnit>(
key: Key(value.id.toString()),
value: value,
child: Text(value.name),
);
}).toList(),
);
}
}
class WeightInputWidget extends ConsumerStatefulWidget {
final num valueChange;
final TextEditingController? controller;
const WeightInputWidget({
super.key,
this.controller,
num? valueChange,
}) : valueChange = valueChange ?? 1.25;
@override
ConsumerState<WeightInputWidget> createState() => _WeightInputWidgetState();
}
class _WeightInputWidgetState extends ConsumerState<WeightInputWidget> {
final _logger = Logger('LogsWeightWidget');
late TextEditingController _controller;
num? _lastWeight;
@override
void initState() {
super.initState();
_controller = widget.controller ?? TextEditingController();
}
@override
void dispose() {
if (widget.controller == null) {
_controller.dispose();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
final numberFormat = NumberFormat.decimalPattern(Localizations.localeOf(context).toString());
final i18n = AppLocalizations.of(context);
final plateProvider = ref.read(plateCalculatorProvider.notifier);
final logProvider = ref.read(gymLogProvider.notifier);
final log = ref.watch(gymLogProvider);
final currentWeight = log?.weight;
// Only update when provider value changed
if (currentWeight != _lastWeight) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) {
return;
}
if (currentWeight != null) {
_controller.text = numberFormat.format(currentWeight);
} else {
_controller.clear();
}
_lastWeight = currentWeight;
});
}
return Row(
children: [
IconButton(
// "Quick-remove" button
icon: const Icon(Icons.remove, color: Colors.black),
onPressed: () {
final base = currentWeight ?? 0;
final newValue = base - widget.valueChange;
if (newValue >= 0 && log != null) {
plateProvider.setWeight(newValue);
logProvider.setWeight(newValue);
}
},
),
// Text field
Expanded(
child: TextFormField(
controller: _controller,
decoration: InputDecoration(labelText: i18n.weight),
keyboardType: textInputTypeDecimal,
onChanged: (value) {
try {
final newValue = numberFormat.parse(value);
plateProvider.setWeight(newValue);
logProvider.setWeight(newValue);
} on FormatException catch (error) {
_logger.finer('Error parsing weight: $error');
}
},
onSaved: (newValue) {
if (newValue == null || log == null) {
return;
}
logProvider.setWeight(numberFormat.parse(newValue));
},
validator: (value) {
if (numberFormat.tryParse(value ?? '') == null) {
return i18n.enterValidNumber;
}
return null;
},
),
),
// "Quick-add" button
IconButton(
icon: const Icon(Icons.add, color: Colors.black),
onPressed: () {
final base = currentWeight ?? 0;
final newValue = base + widget.valueChange;
if (log != null) {
plateProvider.setWeight(newValue);
logProvider.setWeight(newValue);
}
},
),
],
);
}
}