mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 17:31:01 +01:00
feat(measurements): remove date restriction to allow multiple entries per day
This commit is contained in:
@@ -158,163 +158,4 @@ class MeasurementEntryForm extends StatelessWidget {
|
||||
}
|
||||
|
||||
_valueController.text = '';
|
||||
_notesController.text = _entryData['notes']!;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode);
|
||||
|
||||
final measurementProvider = Provider.of<MeasurementProvider>(context, listen: false);
|
||||
final measurementCategory = measurementProvider.categories.firstWhere(
|
||||
(category) => category.id == _categoryId,
|
||||
);
|
||||
|
||||
if (_dateController.text.isEmpty) {
|
||||
_dateController.text = dateFormat.format(_entryData['date']);
|
||||
}
|
||||
|
||||
final numberFormat = NumberFormat.decimalPattern(Localizations.localeOf(context).toString());
|
||||
|
||||
// If the value is not empty, format it
|
||||
if (_valueController.text.isEmpty && _entryData['value'] != null && _entryData['value'] != '') {
|
||||
_valueController.text = numberFormat.format(_entryData['value']);
|
||||
}
|
||||
|
||||
return Form(
|
||||
key: _form,
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
decoration: InputDecoration(
|
||||
labelText: AppLocalizations.of(context).date,
|
||||
suffixIcon: const Icon(
|
||||
Icons.calendar_today,
|
||||
key: Key('calendarIcon'),
|
||||
),
|
||||
),
|
||||
readOnly: true,
|
||||
// Hide text cursor
|
||||
controller: _dateController,
|
||||
onTap: () async {
|
||||
// Stop keyboard from appearing
|
||||
FocusScope.of(context).requestFocus(FocusNode());
|
||||
|
||||
// Show Date Picker Here
|
||||
final pickedDate = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: _entryData['date'],
|
||||
firstDate: DateTime(DateTime.now().year - 10),
|
||||
lastDate: DateTime.now(),
|
||||
|
||||
// TODO(x): we need to filter out dates that already have an entry
|
||||
selectableDayPredicate: (day) {
|
||||
// Always allow the current initial date
|
||||
if (day == _entryData['date']) {
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
);
|
||||
|
||||
_dateController.text = pickedDate == null ? '' : dateFormat.format(pickedDate);
|
||||
},
|
||||
onSaved: (newValue) {
|
||||
_entryData['date'] = dateFormat.parse(newValue!);
|
||||
},
|
||||
validator: (value) {
|
||||
if (value!.isEmpty) {
|
||||
return AppLocalizations.of(context).enterValue;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
// Value
|
||||
TextFormField(
|
||||
decoration: InputDecoration(
|
||||
labelText: AppLocalizations.of(context).value,
|
||||
suffixIcon: Text(measurementCategory.unit),
|
||||
suffixIconConstraints: const BoxConstraints(minWidth: 0, minHeight: 0),
|
||||
),
|
||||
controller: _valueController,
|
||||
keyboardType: textInputTypeDecimal,
|
||||
validator: (value) {
|
||||
if (value!.isEmpty) {
|
||||
return AppLocalizations.of(context).enterValue;
|
||||
}
|
||||
try {
|
||||
numberFormat.parse(value);
|
||||
} catch (error) {
|
||||
return AppLocalizations.of(context).enterValidNumber;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (newValue) {
|
||||
_entryData['value'] = numberFormat.parse(newValue!);
|
||||
},
|
||||
),
|
||||
// Value
|
||||
TextFormField(
|
||||
decoration: InputDecoration(labelText: AppLocalizations.of(context).notes),
|
||||
controller: _notesController,
|
||||
onSaved: (newValue) {
|
||||
_entryData['notes'] = newValue;
|
||||
},
|
||||
validator: (value) {
|
||||
const minLength = 0;
|
||||
const maxLength = 100;
|
||||
if (value!.isNotEmpty && (value.length < minLength || value.length > maxLength)) {
|
||||
return AppLocalizations.of(context).enterCharacters(
|
||||
minLength.toString(),
|
||||
maxLength.toString(),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
|
||||
ElevatedButton(
|
||||
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
|
||||
_entryData['id'] == null
|
||||
? await Provider.of<MeasurementProvider>(
|
||||
context,
|
||||
listen: false,
|
||||
).addEntry(
|
||||
MeasurementEntry(
|
||||
id: _entryData['id'],
|
||||
category: _entryData['category'],
|
||||
date: _entryData['date'],
|
||||
value: _entryData['value'],
|
||||
notes: _entryData['notes'],
|
||||
),
|
||||
)
|
||||
: await Provider.of<MeasurementProvider>(
|
||||
context,
|
||||
listen: false,
|
||||
).editEntry(
|
||||
_entryData['id'],
|
||||
_entryData['category'],
|
||||
_entryData['value'],
|
||||
_entryData['notes'],
|
||||
_entryData['date'],
|
||||
);
|
||||
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
_notesCon
|
||||
Reference in New Issue
Block a user