diff --git a/lib/widgets/measurements/forms.dart b/lib/widgets/measurements/forms.dart index 621253f8c..ba9413e5d 100644 --- a/lib/widgets/measurements/forms.dart +++ b/lib/widgets/measurements/forms.dart @@ -158,4 +158,156 @@ class MeasurementEntryForm extends StatelessWidget { } _valueController.text = ''; - _notesCon \ No newline at end of file + _notesController.text = _entryData['notes']!; + } + + @override + Widget build(BuildContext context) { + final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode); + + final measurementProvider = Provider.of(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(), + + + ); + + _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( + context, + listen: false, + ).addEntry( + MeasurementEntry( + id: _entryData['id'], + category: _entryData['category'], + date: _entryData['date'], + value: _entryData['value'], + notes: _entryData['notes'], + ), + ) + : await Provider.of( + context, + listen: false, + ).editEntry( + _entryData['id'], + _entryData['category'], + _entryData['value'], + _entryData['notes'], + _entryData['date'], + ); + + if (context.mounted) { + Navigator.of(context).pop(); + } + }, + ), + ], + ), + ); + } +} \ No newline at end of file