Files
flutter/lib/widgets/weight/forms.dart
Roland Geider 98f2fb0096 Merge branch 'master' into powersync
# Conflicts:
#	Gemfile.lock
#	android/settings.gradle
#	fastlane/report.xml
#	integration_test/1_dashboard.dart
#	integration_test/2_workout.dart
#	integration_test/3_gym_mode.dart
#	integration_test/5_nutritional_plan.dart
#	integration_test/6_weight.dart
#	lib/helpers/i18n.dart
#	lib/helpers/ui.dart
#	lib/main.dart
#	lib/models/nutrition/meal.g.dart
#	lib/models/nutrition/nutritional_plan.dart
#	lib/providers/auth.dart
#	lib/providers/nutrition.dart
#	lib/screens/add_exercise_screen.dart
#	lib/screens/auth_screen.dart
#	lib/screens/dashboard.dart
#	lib/screens/exercises_screen.dart
#	lib/screens/home_tabs_screen.dart
#	lib/screens/log_meal_screen.dart
#	lib/screens/log_meals_screen.dart
#	lib/screens/nutritional_plan_screen.dart
#	lib/screens/routine_list_screen.dart
#	lib/screens/workout_plan_screen.dart
#	lib/widgets/add_exercise/steps/step1basics.dart
#	lib/widgets/add_exercise/steps/step5images.dart
#	lib/widgets/add_exercise/steps/step_2_variations.dart
#	lib/widgets/add_exercise/steps/step_3_description.dart
#	lib/widgets/add_exercise/steps/step_4_translations.dart
#	lib/widgets/core/about.dart
#	lib/widgets/core/settings.dart
#	lib/widgets/dashboard/calendar.dart
#	lib/widgets/dashboard/widgets.dart
#	lib/widgets/exercises/exercises.dart
#	lib/widgets/gallery/overview.dart
#	lib/widgets/measurements/categories_card.dart
#	lib/widgets/measurements/charts.dart
#	lib/widgets/measurements/entries.dart
#	lib/widgets/measurements/forms.dart
#	lib/widgets/measurements/helpers.dart
#	lib/widgets/nutrition/charts.dart
#	lib/widgets/nutrition/forms.dart
#	lib/widgets/nutrition/helpers.dart
#	lib/widgets/nutrition/meal.dart
#	lib/widgets/nutrition/nutritional_diary_table.dart
#	lib/widgets/nutrition/nutritional_plan_detail.dart
#	lib/widgets/nutrition/nutritional_plans_list.dart
#	lib/widgets/nutrition/widgets.dart
#	lib/widgets/routines/charts.dart
#	lib/widgets/routines/workout_logs.dart
#	lib/widgets/weight/forms.dart
#	lib/widgets/weight/weight_overview.dart
#	lib/widgets/workouts/app_bar.dart
#	lib/widgets/workouts/day.dart
#	lib/widgets/workouts/forms.dart
#	lib/widgets/workouts/gym_mode.dart
#	lib/widgets/workouts/workout_plan_detail.dart
#	lib/widgets/workouts/workout_plans_list.dart
#	linux/flutter/generated_plugin_registrant.cc
#	linux/flutter/generated_plugins.cmake
#	pubspec.lock
#	pubspec.yaml
#	test/auth/auth_screen_test.dart
#	test/core/settings_test.dart
#	test/core/settings_test.mocks.dart
#	test/nutrition/nutritional_meal_form_test.mocks.dart
#	test/nutrition/nutritional_meal_item_form_test.dart
#	test/nutrition/nutritional_plan_form_test.mocks.dart
#	test/nutrition/nutritional_plan_screen_test.dart
#	test/nutrition/nutritional_plans_screen_test.dart
#	test/routine/repetition_unit_form_widget_test.dart
#	test/routine/routine_screen_test.dart
#	test/routine/routines_screen_test.dart
#	test/routine/weight_unit_form_widget_test.dart
#	test/workout/gym_mode_screen_test.dart
#	test/workout/workout_day_form_test.dart
#	test/workout/workout_form_test.dart
#	test/workout/workout_set_form_test.dart
2025-10-19 14:48:15 +02:00

224 lines
8.3 KiB
Dart

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (C) 2020, 2021 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.
*
* wger Workout Manager 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:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/body_weight/weight_entry.dart';
import 'package:wger/providers/body_weight.dart';
class WeightForm extends StatelessWidget {
final _form = GlobalKey<FormState>();
final dateController = TextEditingController(text: '');
final timeController = TextEditingController(text: '');
final weightController = TextEditingController(text: '');
final WeightEntry _weightEntry;
WeightForm([WeightEntry? weightEntry])
: _weightEntry = weightEntry ?? WeightEntry(date: DateTime.now());
@override
Widget build(BuildContext context) {
final numberFormat = NumberFormat.decimalPattern(Localizations.localeOf(context).toString());
final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode);
final timeFormat = DateFormat.Hm(Localizations.localeOf(context).languageCode);
if (weightController.text.isEmpty && _weightEntry.weight != 0) {
weightController.text = numberFormat.format(_weightEntry.weight);
}
if (dateController.text.isEmpty) {
dateController.text = dateFormat.format(_weightEntry.date);
}
if (timeController.text.isEmpty) {
timeController.text = TimeOfDay.fromDateTime(_weightEntry.date).format(context);
}
return Form(
key: _form,
child: Column(
children: [
TextFormField(
key: const Key('dateInput'),
// Stop keyboard from appearing
readOnly: true,
decoration: InputDecoration(
labelText: AppLocalizations.of(context).date,
suffixIcon: const Icon(Icons.calendar_today, key: Key('calendarIcon')),
),
enableInteractiveSelection: false,
controller: dateController,
onTap: () async {
final pickedDate = await showDatePicker(
context: context,
initialDate: _weightEntry.date,
firstDate: DateTime(DateTime.now().year - 10),
lastDate: DateTime.now(),
);
if (pickedDate != null) {
dateController.text = dateFormat.format(pickedDate);
}
},
onSaved: (newValue) {
final date = dateFormat.parse(newValue!);
_weightEntry.date = _weightEntry.date.copyWith(
year: date.year,
month: date.month,
day: date.day,
);
},
),
TextFormField(
key: const Key('timeInput'),
// Stop keyboard from appearing
readOnly: true,
decoration: InputDecoration(
labelText: AppLocalizations.of(context).time,
suffixIcon: const Icon(
Icons.access_time_outlined,
key: Key('clockIcon'),
),
),
enableInteractiveSelection: false,
controller: timeController,
onTap: () async {
final pickedTime = await showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(_weightEntry.date),
);
if (pickedTime != null) {
timeController.text = pickedTime.format(context);
}
},
onSaved: (newValue) {
final time = timeFormat.parse(newValue!);
_weightEntry.date = _weightEntry.date.copyWith(
hour: time.hour,
minute: time.minute,
second: time.second,
);
},
),
// Weight
TextFormField(
key: const Key('weightInput'),
decoration: InputDecoration(
labelText: AppLocalizations.of(context).weight,
prefix: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
key: const Key('quickMinus'),
icon: const FaIcon(FontAwesomeIcons.circleMinus),
onPressed: () {
try {
final newValue = numberFormat.parse(weightController.text) - 1;
weightController.text = numberFormat.format(newValue);
} on FormatException {}
},
),
IconButton(
key: const Key('quickMinusSmall'),
icon: const FaIcon(FontAwesomeIcons.minus),
onPressed: () {
try {
final newValue = numberFormat.parse(weightController.text) - 0.1;
weightController.text = numberFormat.format(newValue);
} on FormatException {}
},
),
],
),
suffix: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
key: const Key('quickPlusSmall'),
icon: const FaIcon(FontAwesomeIcons.plus),
onPressed: () {
try {
final newValue = numberFormat.parse(weightController.text) + 0.1;
weightController.text = numberFormat.format(newValue);
} on FormatException {}
},
),
IconButton(
key: const Key('quickPlus'),
icon: const FaIcon(FontAwesomeIcons.circlePlus),
onPressed: () {
try {
final newValue = numberFormat.parse(weightController.text) + 1;
weightController.text = numberFormat.format(newValue);
} on FormatException {}
},
),
],
),
),
controller: weightController,
keyboardType: textInputTypeDecimal,
onSaved: (newValue) {
_weightEntry.weight = numberFormat.parse(newValue!);
},
validator: (value) {
if (value!.isEmpty) {
return AppLocalizations.of(context).enterValue;
}
try {
numberFormat.parse(value);
} catch (error) {
return AppLocalizations.of(context).enterValidNumber;
}
return null;
},
),
ElevatedButton(
key: const Key(SUBMIT_BUTTON_KEY_NAME),
child: Text(AppLocalizations.of(context).save),
onPressed: () async {
// Validate and save the current values to the weightEntry
final isValid = _form.currentState!.validate();
if (!isValid) {
return;
}
_form.currentState!.save();
// Save the entry on the server
final provider = Provider.of<BodyWeightProvider>(context, listen: false);
_weightEntry.id == null
? await provider.addEntry(_weightEntry)
: await provider.editEntry(_weightEntry);
if (context.mounted) {
Navigator.of(context).pop();
}
},
),
],
),
);
}
}