mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 09:20:56 +01:00
Consolidate usage of DateInputWidget
This commit is contained in:
@@ -136,6 +136,7 @@ class DateInputWidget extends StatefulWidget {
|
||||
this.lastDate,
|
||||
this.onCleared,
|
||||
this.validator,
|
||||
this.helperText,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@@ -147,6 +148,9 @@ class DateInputWidget extends StatefulWidget {
|
||||
|
||||
final String labelText;
|
||||
|
||||
/// Optional hint shown below the field.
|
||||
final String? helperText;
|
||||
|
||||
/// Earliest selectable date. Defaults to the year 2000.
|
||||
final DateTime? firstDate;
|
||||
|
||||
@@ -201,6 +205,7 @@ class _DateInputWidgetState extends State<DateInputWidget> {
|
||||
validator: widget.validator,
|
||||
decoration: InputDecoration(
|
||||
labelText: widget.labelText,
|
||||
helperText: widget.helperText,
|
||||
errorMaxLines: 2,
|
||||
suffixIcon: widget.onCleared != null && _value != null
|
||||
? IconButton(
|
||||
|
||||
@@ -21,12 +21,12 @@ import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/gallery/image.dart';
|
||||
import 'package:wger/providers/gallery_notifier.dart';
|
||||
import 'package:wger/providers/network_provider.dart';
|
||||
import 'package:wger/widgets/core/datetime_input.dart';
|
||||
import 'package:wger/widgets/core/form_submit_button.dart';
|
||||
import 'package:wger/widgets/core/wger_image.dart';
|
||||
|
||||
@@ -46,12 +46,10 @@ class _ImageFormState extends ConsumerState<ImageForm> {
|
||||
|
||||
XFile? _file;
|
||||
|
||||
final dateController = TextEditingController(text: '');
|
||||
final TextEditingController descriptionController = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
dateController.dispose();
|
||||
descriptionController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
@@ -102,17 +100,12 @@ class _ImageFormState extends ConsumerState<ImageForm> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode);
|
||||
final isOnline = ref.watch(networkStatusProvider);
|
||||
// Creating an image or replacing its photo is a binary REST upload and
|
||||
// needs connectivity. A metadata-only edit syncs through PowerSync and
|
||||
// works offline.
|
||||
final requiresUpload = widget._image.id == null || _file != null;
|
||||
|
||||
if (dateController.text.isEmpty) {
|
||||
dateController.text = dateFormat.format(widget._image.date);
|
||||
}
|
||||
|
||||
return Form(
|
||||
key: _form,
|
||||
child: Column(
|
||||
@@ -155,38 +148,17 @@ class _ImageFormState extends ConsumerState<ImageForm> {
|
||||
child: getPicture(),
|
||||
),
|
||||
),
|
||||
TextFormField(
|
||||
DateInputWidget(
|
||||
key: const Key('field-date'),
|
||||
decoration: InputDecoration(
|
||||
labelText: AppLocalizations.of(context).date,
|
||||
suffixIcon: const Icon(Icons.calendar_today),
|
||||
),
|
||||
readOnly: true,
|
||||
// Stop keyboard from appearing
|
||||
controller: dateController,
|
||||
onTap: () async {
|
||||
// Stop keyboard from appearing
|
||||
FocusScope.of(context).requestFocus(FocusNode());
|
||||
|
||||
// Show Date Picker Here
|
||||
final pickedDate = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: widget._image.date,
|
||||
firstDate: DateTime.now().subtract(const Duration(days: 3000)),
|
||||
lastDate: DateTime.now(),
|
||||
);
|
||||
if (pickedDate != null) {
|
||||
dateController.text = dateFormat.format(pickedDate);
|
||||
}
|
||||
},
|
||||
onSaved: (newValue) {
|
||||
widget._image.date = dateFormat.parse(newValue!);
|
||||
},
|
||||
value: widget._image.date,
|
||||
labelText: AppLocalizations.of(context).date,
|
||||
firstDate: DateTime.now().subtract(const Duration(days: 3000)),
|
||||
lastDate: DateTime.now(),
|
||||
onChanged: (date) => widget._image.date = date,
|
||||
validator: (value) {
|
||||
if (widget._image.id == null && _file == null) {
|
||||
return AppLocalizations.of(context).selectImage;
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
),
|
||||
|
||||
@@ -496,7 +496,6 @@ class _PlanFormState extends ConsumerState<PlanForm> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode);
|
||||
final isCreating = widget._plan.id == null;
|
||||
|
||||
return Form(
|
||||
@@ -526,43 +525,22 @@ class _PlanFormState extends ConsumerState<PlanForm> {
|
||||
},
|
||||
),
|
||||
// Start Date
|
||||
TextFormField(
|
||||
DateInputWidget(
|
||||
key: const Key('field-start-date'),
|
||||
decoration: InputDecoration(
|
||||
labelText: AppLocalizations.of(context).startDate,
|
||||
suffixIcon: const Icon(
|
||||
Icons.calendar_today,
|
||||
key: Key('calendarIcon'),
|
||||
),
|
||||
),
|
||||
controller: TextEditingController(
|
||||
text: dateFormat.format(widget._plan.startDate),
|
||||
),
|
||||
readOnly: true,
|
||||
onTap: () async {
|
||||
// Stop keyboard from appearing
|
||||
FocusScope.of(context).requestFocus(FocusNode());
|
||||
|
||||
// Open date picker
|
||||
final pickedDate = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: widget._plan.startDate,
|
||||
firstDate: DateTime(2000),
|
||||
lastDate: DateTime(2100),
|
||||
);
|
||||
|
||||
if (pickedDate != null) {
|
||||
setState(() {
|
||||
widget._plan.startDate = pickedDate;
|
||||
});
|
||||
}
|
||||
value: widget._plan.startDate,
|
||||
labelText: AppLocalizations.of(context).startDate,
|
||||
firstDate: DateTime(2000),
|
||||
lastDate: DateTime(2100),
|
||||
onChanged: (pickedDate) {
|
||||
setState(() {
|
||||
widget._plan.startDate = pickedDate;
|
||||
});
|
||||
},
|
||||
validator: (value) {
|
||||
if (widget._plan.endDate != null &&
|
||||
widget._plan.endDate!.isBefore(widget._plan.startDate)) {
|
||||
return 'End date must be after start date';
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
),
|
||||
@@ -570,51 +548,23 @@ class _PlanFormState extends ConsumerState<PlanForm> {
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
child: DateInputWidget(
|
||||
key: const Key('field-end-date'),
|
||||
decoration: InputDecoration(
|
||||
labelText: AppLocalizations.of(context).endDate,
|
||||
helperText:
|
||||
'Tip: only for athletes with contest deadlines. Most users benefit from flexibility',
|
||||
suffixIcon: widget._plan.endDate == null
|
||||
? const Icon(
|
||||
Icons.calendar_today,
|
||||
key: Key('calendarIcon'),
|
||||
)
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
tooltip: 'Clear end date',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
widget._plan.endDate = null;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
controller: TextEditingController(
|
||||
text: widget._plan.endDate == null
|
||||
? ''
|
||||
: dateFormat.format(widget._plan.endDate!),
|
||||
),
|
||||
readOnly: true,
|
||||
onTap: () async {
|
||||
// Stop keyboard from appearing
|
||||
FocusScope.of(context).requestFocus(FocusNode());
|
||||
|
||||
// Open date picker
|
||||
final pickedDate = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: widget._plan.endDate,
|
||||
// end must be after start
|
||||
firstDate: widget._plan.startDate.add(const Duration(days: 1)),
|
||||
lastDate: DateTime(2100),
|
||||
);
|
||||
|
||||
if (pickedDate != null) {
|
||||
setState(() {
|
||||
widget._plan.endDate = pickedDate;
|
||||
});
|
||||
}
|
||||
value: widget._plan.endDate,
|
||||
labelText: AppLocalizations.of(context).endDate,
|
||||
helperText:
|
||||
'Tip: only for athletes with contest deadlines. Most users benefit from flexibility',
|
||||
firstDate: widget._plan.startDate.add(const Duration(days: 1)),
|
||||
lastDate: DateTime(2100),
|
||||
onChanged: (pickedDate) {
|
||||
setState(() {
|
||||
widget._plan.endDate = pickedDate;
|
||||
});
|
||||
},
|
||||
onCleared: () {
|
||||
setState(() {
|
||||
widget._plan.endDate = null;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
@@ -18,13 +18,13 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/providers/network_provider.dart';
|
||||
import 'package:wger/providers/routines_notifier.dart';
|
||||
import 'package:wger/screens/routine_edit_screen.dart';
|
||||
import 'package:wger/widgets/core/datetime_input.dart';
|
||||
import 'package:wger/widgets/core/form_submit_button.dart';
|
||||
|
||||
class RoutineForm extends ConsumerStatefulWidget {
|
||||
@@ -107,10 +107,18 @@ class _RoutineFormState extends ConsumerState<RoutineForm> {
|
||||
widget._routine.description = newValue!;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
DateInputWidget(
|
||||
key: const Key('field-start-date'),
|
||||
// Stop keyboard from appearing
|
||||
readOnly: true,
|
||||
value: startDate,
|
||||
labelText: i18n.startDate,
|
||||
firstDate: DateTime.now().subtract(const Duration(days: 365)),
|
||||
lastDate: DateTime.now().add(const Duration(days: 365)),
|
||||
onChanged: (picked) {
|
||||
widget._routine.start = picked;
|
||||
setState(() {
|
||||
startDate = picked;
|
||||
});
|
||||
},
|
||||
validator: (value) {
|
||||
if (endDate.isBefore(startDate)) {
|
||||
return 'End date must be after start date';
|
||||
@@ -123,74 +131,18 @@ class _RoutineFormState extends ConsumerState<RoutineForm> {
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: i18n.startDate,
|
||||
suffixIcon: const Icon(
|
||||
Icons.calendar_today,
|
||||
key: Key('calendarIcon'),
|
||||
),
|
||||
),
|
||||
enableInteractiveSelection: false,
|
||||
controller: TextEditingController(
|
||||
text: DateFormat.yMd(
|
||||
Localizations.localeOf(context).languageCode,
|
||||
).format(startDate),
|
||||
),
|
||||
onTap: () async {
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: startDate,
|
||||
firstDate: DateTime.now().subtract(const Duration(days: 365)),
|
||||
lastDate: DateTime.now().add(const Duration(days: 365)),
|
||||
);
|
||||
|
||||
if (picked == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
widget._routine.start = picked;
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
startDate = picked;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
DateInputWidget(
|
||||
key: const Key('field-end-date'),
|
||||
// Stop keyboard from appearing
|
||||
readOnly: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: i18n.endDate,
|
||||
suffixIcon: const Icon(
|
||||
Icons.calendar_today,
|
||||
key: Key('calendarIcon'),
|
||||
),
|
||||
),
|
||||
enableInteractiveSelection: false,
|
||||
controller: TextEditingController(
|
||||
text: DateFormat.yMd(
|
||||
Localizations.localeOf(context).languageCode,
|
||||
).format(endDate),
|
||||
),
|
||||
onTap: () async {
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: endDate,
|
||||
firstDate: DateTime.now().subtract(const Duration(days: 365)),
|
||||
lastDate: DateTime.now().add(const Duration(days: 365)),
|
||||
);
|
||||
|
||||
if (picked == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
value: endDate,
|
||||
labelText: i18n.endDate,
|
||||
firstDate: DateTime.now().subtract(const Duration(days: 365)),
|
||||
lastDate: DateTime.now().add(const Duration(days: 365)),
|
||||
onChanged: (picked) {
|
||||
widget._routine.end = picked;
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
endDate = picked;
|
||||
});
|
||||
}
|
||||
setState(() {
|
||||
endDate = picked;
|
||||
});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 5),
|
||||
|
||||
Reference in New Issue
Block a user