From 906e92b93614d47de90598aaa222ae40fee0c619 Mon Sep 17 00:00:00 2001 From: "Joons Stuijvenberg, van" Date: Mon, 16 Jan 2023 15:07:01 +0100 Subject: [PATCH] fix date-time format and validator --- CHANGELOG.md | 5 ++- example/lib/main.dart | 13 ++++++- example/pubspec.lock | 2 +- lib/src/inputs/date_picker/date_picker.dart | 38 ++++++++++--------- .../inputs/date_picker/date_picker_field.dart | 14 ++++++- pubspec.yaml | 2 +- 6 files changed, 51 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcb3212..9dab922 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,4 +12,7 @@ ## 1.0.3 -* add FocusNode option for input fields \ No newline at end of file +* add FocusNode option for input fields + +## 1.0.4 +* fix datetimepicker format and validator diff --git a/example/lib/main.dart b/example/lib/main.dart index c423657..64856ff 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -62,8 +62,17 @@ class _MyHomePageState extends State { const Text('FlutterFormInputDateTime'), FlutterFormInputDateTime( decoration: const InputDecoration(label: Text('test')), - inputType: FlutterFormDateTimeType.time, - dateFormat: DateFormat('HH:mm'), + autovalidateMode: AutovalidateMode.always, + validator: (p0) { + if (p0 == null || p0.isEmpty) return null; + return DateFormat('dd/MMMM/yyyy HH:mm') + .parse(p0) + .isAfter(DateTime.now()) + ? null + : 'Date must be in the future'; + }, + inputType: FlutterFormDateTimeType.dateTime, + dateFormat: DateFormat('dd/MMMM/yyyy HH:mm'), onChanged: (v) { debugPrint('Date changed to $v'); }, diff --git a/example/pubspec.lock b/example/pubspec.lock index e2e1418..8c25539 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -61,7 +61,7 @@ packages: path: ".." relative: true source: path - version: "1.0.3" + version: "1.0.4" flutter_lints: dependency: "direct dev" description: diff --git a/lib/src/inputs/date_picker/date_picker.dart b/lib/src/inputs/date_picker/date_picker.dart index e4ee2a1..b12ed72 100644 --- a/lib/src/inputs/date_picker/date_picker.dart +++ b/lib/src/inputs/date_picker/date_picker.dart @@ -15,23 +15,24 @@ enum FlutterFormDateTimeType { } class FlutterFormInputDateTime extends ConsumerWidget { - const FlutterFormInputDateTime({ - this.decoration, - Key? key, - this.label, - this.showIcon = true, - required this.inputType, - required this.dateFormat, - this.firstDate, - this.lastDate, - this.initialDate, - this.initialDateTimeRange, - this.icon = Icons.calendar_today, - this.initialValue, - this.onChanged, - this.onSaved, - this.validator, - }) : super( + const FlutterFormInputDateTime( + {this.decoration, + Key? key, + this.label, + this.showIcon = true, + required this.inputType, + required this.dateFormat, + this.firstDate, + this.lastDate, + this.initialDate, + this.initialDateTimeRange, + this.icon = Icons.calendar_today, + this.initialValue, + this.onChanged, + this.onSaved, + this.validator, + this.autovalidateMode = AutovalidateMode.disabled}) + : super( key: key, ); final InputDecoration? decoration; @@ -48,11 +49,14 @@ class FlutterFormInputDateTime extends ConsumerWidget { final String? Function(String?)? validator; final void Function(String?)? onSaved; final void Function(String?)? onChanged; + final AutovalidateMode autovalidateMode; @override Widget build(BuildContext context, WidgetRef ref) { return DateTimeInputField( decoration: decoration, + autovalidateMode: autovalidateMode, + validator: validator, label: label, icon: icon, firstDate: firstDate, diff --git a/lib/src/inputs/date_picker/date_picker_field.dart b/lib/src/inputs/date_picker/date_picker_field.dart index 5d99b6f..ebfcd83 100644 --- a/lib/src/inputs/date_picker/date_picker_field.dart +++ b/lib/src/inputs/date_picker/date_picker_field.dart @@ -14,6 +14,7 @@ class DateTimeInputField extends ConsumerStatefulWidget { this.decoration, Key? key, required this.inputType, + required this.autovalidateMode, this.label, this.showIcon = true, this.icon, @@ -30,6 +31,7 @@ class DateTimeInputField extends ConsumerStatefulWidget { key: key, ); final InputDecoration? decoration; + final AutovalidateMode autovalidateMode; final FlutterFormDateTimeType inputType; final DateFormat dateFormat; final bool showIcon; @@ -100,7 +102,16 @@ class _DateInputFieldState extends ConsumerState { String secondInput = await getInputFromUser(FlutterFormDateTimeType.time); if (secondInput != '') { - userInput = '$value $secondInput'; + var date = widget.dateFormat.parse(value); + var time = DateFormat('dd MM yyyy hh:mm') + .parse('01 01 1970 $secondInput'); + userInput = widget.dateFormat.format(DateTime( + date.year, + date.month, + date.day, + time.hour, + time.minute, + )); } } }); @@ -138,6 +149,7 @@ class _DateInputFieldState extends ConsumerState { } return TextFormField( + autovalidateMode: widget.autovalidateMode, keyboardType: TextInputType.none, readOnly: true, key: Key(currentValue.toString()), diff --git a/pubspec.yaml b/pubspec.yaml index a129d56..a1cb727 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_input_library description: A new Flutter package project. -version: 1.0.3 +version: 1.0.4 repository: https://github.com/Iconica-Development/flutter_input_library environment: