diff --git a/CHANGELOG.md b/CHANGELOG.md index 686ee92..f0a9bf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +##2.1.2 + +* Adding the possibility to specify the initial date selected in the date picker dialog. + ##2.1.1 * Formatting with Dart FM diff --git a/README.md b/README.md index 77828fe..bd4ed6f 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency: ```yaml dependencies: ... - date_field: ^2.1.0 + date_field: ^2.1.2 ``` In your library add the following import: diff --git a/example/lib/main.dart b/example/lib/main.dart index cadcf4b..026912c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -73,6 +73,8 @@ class _MyHomePageState extends State { suffixIcon: Icon(Icons.event_note), labelText: 'My Super Date Time Field', ), + firstDate: DateTime.now().add(const Duration(days: 10)), + initialDate: DateTime.now().add(const Duration(days: 10)), autovalidateMode: AutovalidateMode.always, validator: (DateTime? e) => (e?.day ?? 0) == 1 ? 'Please not the first day' : null, diff --git a/lib/src/field.dart b/lib/src/field.dart index 5c8abf9..a809da3 100644 --- a/lib/src/field.dart +++ b/lib/src/field.dart @@ -22,6 +22,7 @@ class DateTimeField extends StatelessWidget { this.mode = DateTimeFieldPickerMode.dateAndTime, this.initialEntryMode = DatePickerEntryMode.calendar, this.dateTextStyle, + this.initialDate, DateTime? firstDate, DateTime? lastDate, DateFormat? dateFormat, @@ -36,6 +37,7 @@ class DateTimeField extends StatelessWidget { this.selectedDate, this.decoration, this.enabled, + this.initialDate, this.dateTextStyle, this.initialEntryMode = DatePickerEntryMode.calendar, DateTime? firstDate, @@ -59,6 +61,9 @@ class DateTimeField extends StatelessWidget { /// The last date that the user can select (default is 2100) final DateTime lastDate; + /// The date that will be selected by default in the calendar view. + final DateTime? initialDate; + /// Let you choose the [DatePickerMode] for the date picker! (default is [DatePickerMode.day] final DatePickerMode? initialDatePickerMode; @@ -82,7 +87,18 @@ class DateTimeField extends StatelessWidget { /// Shows a dialog asking the user to pick a date ! Future _selectDate(BuildContext context) async { - final DateTime initialDateTime = selectedDate ?? DateTime.now(); + final DateTime initialDateTime; + + if (selectedDate != null) { + initialDateTime = selectedDate!; + } else { + final DateTime now = DateTime.now(); + if (firstDate.isAfter(now) || lastDate.isBefore(now)) { + initialDateTime = initialDate ?? lastDate; + } else { + initialDateTime = now; + } + } if (Theme.of(context).platform == TargetPlatform.iOS) { showModalBottomSheet( diff --git a/lib/src/form_field.dart b/lib/src/form_field.dart index 45afc4c..3df9c9f 100644 --- a/lib/src/form_field.dart +++ b/lib/src/form_field.dart @@ -24,6 +24,7 @@ class DateTimeFormField extends FormField { DateFormat? dateFormat, DateTime? firstDate, DateTime? lastDate, + DateTime? initialDate, ValueChanged? onDateSelected, InputDecoration? decoration, DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar, @@ -54,6 +55,7 @@ class DateTimeFormField extends FormField { return DateTimeField( firstDate: firstDate, + initialDate: initialDate, lastDate: lastDate, decoration: effectiveDecoration, initialDatePickerMode: initialDatePickerMode, diff --git a/pubspec.yaml b/pubspec.yaml index f98ff9e..80005cb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: date_field description: A widget in the form of a field that lets people choose a date, a time or both. -version: 2.1.1 +version: 2.1.2 homepage: 'https://github.com/GaspardMerten/date_field' environment: