date_time_picker
a flutter widget to show a text form field to display a date or clock dialog. this widget extend textfield and has a similar behavior as textformfield.
usage
in the pubspec.yaml
of your flutter project, add the following dependency:
dependencies:
...
date_time_picker: "^1.0.2"
in your library add the following import:
import 'package:date_time_picker/date_time_picker.dart';
for help getting started with flutter, view the online documentation.
example
there are four presentations for datetimepicker and can be defined in the type parameter:
datetimepickertype.date
will present a text field with the action tap showing a datepicker dialog box;datetimepickertype.time
will present a text field with the action tap showing a timepicker dialog box;datetimepickertype.datetime
will present a text field with the action tap showing a datepicker dialog box then a timepicker dialog box;datetimepickertype.datetimeseparated
will display two text fields side by side, the first for date and the second for time. each displaying their respective dialog box, datepicker and timepicker in the tap action;
datetimepicker(
type: date, // options: [date | time | datetime | datetimeseparated], default is date
...
)
initialvalue or controller.text can be null
, empty
or a datetime string
otherwise it will throw an error.
datetimepicker(
initialvalue: '',
firstdate: datetime(2000),
lastdate: datetime(2100),
datelabeltext: 'date',
onchanged: (val) => print(val),
validator: (val) {
print(val);
return null;
},
onsaved: (val) => print(val),
);
more complete example:
datetimepicker(
type: datetimepickertype.datetimeseparate,
datemask: 'd mmm, yyyy',
initialvalue: datetime.now().tostring(),
firstdate: datetime(2000),
lastdate: datetime(2100),
icon: icon(icons.event),
datelabeltext: 'date',
timelabeltext: "hour",
selectabledaypredicate: (date) {
// disable weekend days to select from the calendar
if (date.weekday == 6 || date.weekday == 7) {
return false;
}
return true;
},
onchanged: (val) => print(val),
validator: (val) {
print(val);
return null;
},
onsaved: (val) => print(val),
);
the result of val in onchanged
, validator
and onsaved
will be a datetime string or just a time string:
- ex.: [2020-07-20 14:30] or [15:30] datetimepickertype.time;
- month, day, hour and minute will be 2 digits and time always be in 24 hours mode;
- but the presentation in text field can be formated by the datemask parameter.
Comments are closed.