showSimpleDatePicker static method
Future<DateTime?>
showSimpleDatePicker(
- BuildContext context, {
- DateTime? firstDate,
- DateTime? lastDate,
- DateTime? initialDate,
- String? dateFormat,
- DateTimePickerLocale locale = DATETIME_PICKER_LOCALE_DEFAULT,
- DateTimePickerMode pickerMode = DateTimePickerMode.date,
- Color? backgroundColor,
- Color? textColor,
- TextStyle? itemTextStyle,
- String? titleText,
- String? confirmText,
- String? cancelText,
- bool looping = false,
- bool reverse = false,
Display date picker in bottom sheet.
context: BuildContext firstDate: DateTime minimum date time lastDate: DateTime maximum date time initialDateTime: DateTime initial date time for selected dateFormat: String date format pattern locale: DateTimePickerLocale internationalization backgroundColor: Color background color of the dialog itemTextStyle: TextStyle item TextStyle of the picker titleText: String text of the dialog's title confirmText: String text of the dialog's confirm button cancelText: String text of the dialog's cancel button
Implementation
static Future<DateTime?> showSimpleDatePicker(
BuildContext context, {
DateTime? firstDate,
DateTime? lastDate,
DateTime? initialDate,
String? dateFormat,
DateTimePickerLocale locale = DATETIME_PICKER_LOCALE_DEFAULT,
DateTimePickerMode pickerMode = DateTimePickerMode.date,
Color? backgroundColor,
Color? textColor,
TextStyle? itemTextStyle,
String? titleText,
String? confirmText,
String? cancelText,
bool looping = false,
bool reverse = false,
}) {
DateTime? _selectedDate = initialDate ?? DateTime.now().startOfDay();
final List<Widget> listButtonActions = [
TextButton(
style: TextButton.styleFrom(foregroundColor: textColor),
child: Text(confirmText ?? "OK"),
onPressed: () {
Navigator.pop(context, _selectedDate);
},
),
TextButton(
style: TextButton.styleFrom(foregroundColor: textColor),
child: Text(cancelText ?? "Cancel"),
onPressed: () {
Navigator.pop(context);
},
)
];
// handle the range of datetime
if (firstDate == null) {
firstDate = DateTime.parse(DATE_PICKER_MIN_DATETIME);
}
if (lastDate == null) {
lastDate = DateTime.parse(DATE_PICKER_MAX_DATETIME);
}
// handle initial DateTime
if (initialDate == null) {
initialDate = DateTime.now();
}
if (backgroundColor == null)
backgroundColor = DateTimePickerTheme.Default.backgroundColor;
// if (itemTextStyle == null)
// itemTextStyle = DateTimePickerTheme.Default.itemTextStyle;
if (textColor == null)
textColor = DateTimePickerTheme.Default.itemTextStyle.color;
var datePickerDialog = AlertDialog(
title: Text(
titleText ?? "Select Date",
style: TextStyle(color: textColor),
),
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 14),
backgroundColor: backgroundColor,
content: Container(
width: 300,
child: DatePickerWidget(
firstDate: firstDate,
lastDate: lastDate,
initialDate: initialDate,
dateFormat: dateFormat,
locale: locale,
pickerTheme: DateTimePickerTheme(
backgroundColor: backgroundColor,
itemTextStyle: itemTextStyle ?? TextStyle(color: textColor),
),
onChange: ((DateTime date, list) {
print(date);
_selectedDate = date;
}),
looping: looping,
),
),
actions:
reverse ? listButtonActions.reversed.toList() : listButtonActions,
);
return showDialog(
useRootNavigator: false,
context: context,
builder: (context) => datePickerDialog);
}