Adding the possibility to specify the initial date selected in the date picker dialog.

This commit is contained in:
Gaspard Merten 2021-09-25 18:08:03 +02:00
parent d3fe446cdc
commit bf47b6c6c6
6 changed files with 27 additions and 3 deletions

View file

@ -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

View file

@ -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:

View file

@ -73,6 +73,8 @@ class _MyHomePageState extends State<MyHomePage> {
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,

View file

@ -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<void> _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<void>(

View file

@ -24,6 +24,7 @@ class DateTimeFormField extends FormField<DateTime> {
DateFormat? dateFormat,
DateTime? firstDate,
DateTime? lastDate,
DateTime? initialDate,
ValueChanged<DateTime>? onDateSelected,
InputDecoration? decoration,
DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
@ -54,6 +55,7 @@ class DateTimeFormField extends FormField<DateTime> {
return DateTimeField(
firstDate: firstDate,
initialDate: initialDate,
lastDate: lastDate,
decoration: effectiveDecoration,
initialDatePickerMode: initialDatePickerMode,

View file

@ -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: