From 726ec9ca6e00a38a5b7738295931ac7f5b47ebab Mon Sep 17 00:00:00 2001 From: Martin Kamleithner Date: Tue, 16 Jul 2019 18:32:20 +0200 Subject: [PATCH 1/3] upgrade to datetime_picker_formfield 0.3.2 --- example/lib/main.dart | 57 ++++++------ .../fields/form_builder_date_time_picker.dart | 93 ++++++++++++------- pubspec.lock | 20 ++-- pubspec.yaml | 2 +- 4 files changed, 100 insertions(+), 72 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index d0d4da9104..e49943f0b5 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,4 +1,3 @@ -import 'package:datetime_picker_formfield/datetime_picker_formfield.dart'; import 'package:flutter/material.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; import 'package:intl/intl.dart'; @@ -149,7 +148,7 @@ class MyHomePageState extends State { FormBuilderDateTimePicker( attribute: "date", onChanged: _onChanged, - inputType: InputType.date, + // inputType: InputType.date, format: DateFormat("yyyy-MM-dd"), decoration: InputDecoration(labelText: "Appointment Time"), @@ -333,34 +332,34 @@ class MyHomePageState extends State { children: languages .map( (lang) => Row( - children: [ - Radio( - value: lang, - groupValue: field.value, - onChanged: (dynamic value) { - field.didChange(lang); - }, - ), - lang != "Other" - ? Text(lang) - : Expanded( - child: Row( - children: [ - Text( - lang, - ), - SizedBox(width: 20), - Expanded( - child: TextFormField( - key: - _specifyTextFieldKey, - ), - ), - ], - ), - ), - ], + children: [ + Radio( + value: lang, + groupValue: field.value, + onChanged: (dynamic value) { + field.didChange(lang); + }, ), + lang != "Other" + ? Text(lang) + : Expanded( + child: Row( + children: [ + Text( + lang, + ), + SizedBox(width: 20), + Expanded( + child: TextFormField( + key: + _specifyTextFieldKey, + ), + ), + ], + ), + ), + ], + ), ) .toList(growable: false), ), diff --git a/lib/src/fields/form_builder_date_time_picker.dart b/lib/src/fields/form_builder_date_time_picker.dart index d6e63458f0..01bc71975d 100644 --- a/lib/src/fields/form_builder_date_time_picker.dart +++ b/lib/src/fields/form_builder_date_time_picker.dart @@ -7,6 +7,8 @@ import 'package:flutter/services.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; import 'package:intl/intl.dart'; +enum InputType { date, time, both } + class FormBuilderDateTimePicker extends StatefulWidget { final String attribute; final List validators; @@ -184,19 +186,12 @@ class _FormBuilderDateTimePickerState extends State { Widget build(BuildContext context) { _readonly = (_formState?.readonly == true) ? true : widget.readonly; - return DateTimePickerFormField( + return DateTimeField( key: _fieldKey, - inputType: widget.inputType, initialValue: widget.initialValue, format: widget.format != null ? widget.format : _dateTimeFormats[widget.inputType], - enabled: !_readonly, - firstDate: widget.firstDate, - lastDate: widget.lastDate, - decoration: widget.decoration.copyWith( - enabled: !_readonly, - ), onSaved: (val) { if (widget.valueTransformer != null) { var transformed = widget.valueTransformer(val); @@ -212,31 +207,65 @@ class _FormBuilderDateTimePickerState extends State { return null; }, onChanged: widget.onChanged, - builder: widget.builder, - datePicker: widget.datePicker, - timePicker: widget.timePicker, - onFieldSubmitted: widget.onFieldSubmitted, autovalidate: widget.autovalidate, - style: widget.style, - textDirection: widget.textDirection, - textAlign: widget.textAlign, - maxLength: widget.maxLength, - autofocus: widget.autofocus, - autocorrect: widget.autocorrect, - controller: widget.controller, - editable: widget.editable, - focusNode: widget.focusNode, - initialDate: widget.initialDate, - initialDatePickerMode: widget.initialDatePickerMode, - initialTime: widget.initialTime, - inputFormatters: widget.inputFormatters, - keyboardType: widget.keyboardType, - locale: widget.locale, - maxLengthEnforced: widget.maxLengthEnforced, - maxLines: widget.maxLines, - obscureText: widget.obscureText, - resetIcon: widget.resetIcon, - selectableDayPredicate: widget.selectableDayPredicate, + resetIcon: Icon(widget.resetIcon), + child: TextField( + textDirection: widget.textDirection, + textAlign: widget.textAlign, + maxLength: widget.maxLength, + autofocus: widget.autofocus, + decoration: widget.decoration, + enabled: widget.editable, + autocorrect: widget.autocorrect, + readOnly: widget.editable, + controller: widget.controller, + focusNode: widget.focusNode, + inputFormatters: widget.inputFormatters, + keyboardType: widget.keyboardType, + maxLengthEnforced: widget.maxLengthEnforced, + maxLines: widget.maxLines, + obscureText: widget.obscureText, + ), + onShowPicker: (ctx, time) async { + switch (widget.inputType) { + case InputType.date: + return _showDatePicker(context); + case InputType.time: + return DateTimeField.convert(await _showTimePicker(context)); + case InputType.both: + final date = await _showDatePicker(context); + if (date != null) { + final time = await _showTimePicker(context); + return DateTimeField.combine(date, time); + } + return _fieldKey.currentState.value ?? widget.initialValue; + default: + throw "unexcepted input type ${widget.inputType}"; + } + }, ); } + + Future _showDatePicker(BuildContext context) { + if (widget.datePicker != null) { + return widget.datePicker(context); + } else { + return showDatePicker( + context: context, + selectableDayPredicate: widget.selectableDayPredicate, + initialDatePickerMode: + widget.initialDatePickerMode ?? DatePickerMode.day, + initialDate: widget.initialDate, + firstDate: widget.firstDate, + lastDate: widget.lastDate); + } + } + + Future _showTimePicker(BuildContext context) { + if (widget.timePicker != null) { + return widget.timePicker(context); + } else { + return showTimePicker(context: context, initialTime: widget.initialTime); + } + } } diff --git a/pubspec.lock b/pubspec.lock index 8613c61712..0e37560876 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,5 +1,5 @@ # Generated by pub -# See https://www.dartlang.org/tools/pub/glossary#lockfile +# See https://dart.dev/tools/pub/glossary#lockfile packages: analyzer: dependency: transitive @@ -21,7 +21,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.2.0" boolean_selector: dependency: transitive description: @@ -70,7 +70,7 @@ packages: name: datetime_picker_formfield url: "https://pub.dartlang.org" source: hosted - version: "0.2.0" + version: "0.3.2" flutter: dependency: "direct main" description: flutter @@ -241,7 +241,7 @@ packages: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.5.0" + version: "1.7.0" pool: dependency: transitive description: @@ -262,7 +262,7 @@ packages: name: quiver url: "https://pub.dartlang.org" source: hosted - version: "2.0.2" + version: "2.0.3" shelf: dependency: transitive description: @@ -358,21 +358,21 @@ packages: name: test url: "https://pub.dartlang.org" source: hosted - version: "1.6.1" + version: "1.6.3" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.4" + version: "0.2.5" test_core: dependency: transitive description: name: test_core url: "https://pub.dartlang.org" source: hosted - version: "0.2.3" + version: "0.2.5" typed_data: dependency: transitive description: @@ -423,5 +423,5 @@ packages: source: hosted version: "2.1.15" sdks: - dart: ">=2.2.0 <3.0.0" - flutter: ">=0.1.4 <2.0.0" + dart: ">=2.2.2 <3.0.0" + flutter: ">=1.0.0 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index d68793c3e9..9a1048a734 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ dependencies: intl: ^0.15.8 sy_flutter_widgets: ^0.1.4 flutter_chips_input: ^1.3.0 - datetime_picker_formfield: ^0.2.0 + datetime_picker_formfield: ^0.3.2 # signature: ^2.0.0 - Doesn't implement onChanged, submitted PR. Meanwhile using local clone with onChanged validators: ^2.0.0+1 From 1992e666c5e031bf98142e584f26f42355a159ed Mon Sep 17 00:00:00 2001 From: Martin Kamleithner Date: Tue, 16 Jul 2019 18:36:38 +0200 Subject: [PATCH 2/3] revert example to old state --- example/lib/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index e49943f0b5..339158b94d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -148,7 +148,7 @@ class MyHomePageState extends State { FormBuilderDateTimePicker( attribute: "date", onChanged: _onChanged, - // inputType: InputType.date, + inputType: InputType.date, format: DateFormat("yyyy-MM-dd"), decoration: InputDecoration(labelText: "Appointment Time"), From 28f71c9d182c1f836d88ddafaf374639a73cd1dd Mon Sep 17 00:00:00 2001 From: Martin Kamleithner Date: Wed, 17 Jul 2019 11:17:32 +0200 Subject: [PATCH 3/3] update datetime_picker_formfield to 0.4.0 --- .../fields/form_builder_date_time_picker.dart | 32 +++++++++---------- pubspec.lock | 2 +- pubspec.yaml | 2 +- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/src/fields/form_builder_date_time_picker.dart b/lib/src/fields/form_builder_date_time_picker.dart index 01bc71975d..22934468a3 100644 --- a/lib/src/fields/form_builder_date_time_picker.dart +++ b/lib/src/fields/form_builder_date_time_picker.dart @@ -209,23 +209,21 @@ class _FormBuilderDateTimePickerState extends State { onChanged: widget.onChanged, autovalidate: widget.autovalidate, resetIcon: Icon(widget.resetIcon), - child: TextField( - textDirection: widget.textDirection, - textAlign: widget.textAlign, - maxLength: widget.maxLength, - autofocus: widget.autofocus, - decoration: widget.decoration, - enabled: widget.editable, - autocorrect: widget.autocorrect, - readOnly: widget.editable, - controller: widget.controller, - focusNode: widget.focusNode, - inputFormatters: widget.inputFormatters, - keyboardType: widget.keyboardType, - maxLengthEnforced: widget.maxLengthEnforced, - maxLines: widget.maxLines, - obscureText: widget.obscureText, - ), + textDirection: widget.textDirection, + textAlign: widget.textAlign, + maxLength: widget.maxLength, + autofocus: widget.autofocus, + decoration: widget.decoration, + enabled: widget.editable, + autocorrect: widget.autocorrect, + readOnly: _readonly, + controller: widget.controller, + focusNode: widget.focusNode, + inputFormatters: widget.inputFormatters, + keyboardType: widget.keyboardType, + maxLengthEnforced: widget.maxLengthEnforced, + maxLines: widget.maxLines, + obscureText: widget.obscureText, onShowPicker: (ctx, time) async { switch (widget.inputType) { case InputType.date: diff --git a/pubspec.lock b/pubspec.lock index 0e37560876..ad443766f1 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -70,7 +70,7 @@ packages: name: datetime_picker_formfield url: "https://pub.dartlang.org" source: hosted - version: "0.3.2" + version: "0.4.0" flutter: dependency: "direct main" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 9a1048a734..7eaa21dd26 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ dependencies: intl: ^0.15.8 sy_flutter_widgets: ^0.1.4 flutter_chips_input: ^1.3.0 - datetime_picker_formfield: ^0.3.2 + datetime_picker_formfield: ^0.4.0 # signature: ^2.0.0 - Doesn't implement onChanged, submitted PR. Meanwhile using local clone with onChanged validators: ^2.0.0+1