CreateDatePicker constructor
CreateDatePicker({
- Key? key,
- ViewState initialViewState = ViewState.date,
- DateTime? initialDate,
- DateTime? minDateTime,
- DateTime? maxDateTime,
- List<
DateTime> restrictedDates = const [], - List<
String> weekLabels = const ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], - List<
String> monthLabels = const ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], - double? width,
- double elevation = 1,
- Offset? popupMenuOffset,
- PopupMenuPosition popupMenuPosition = PopupMenuPosition.under,
- dynamic builder()?,
- dynamic leftArrowBuilder(
- dynamic previous()
- dynamic rightArrowBuilder(
- dynamic next()
- dynamic popupSelectedDateBuilder(
- DateTime selectedDate
- dynamic dateViewButtonBuilder()?,
- dynamic monthViewButtonBuilder()?,
- dynamic yearViewButtonBuilder()?,
- dynamic weekdayCellBuilder()?,
- dynamic dateCellBuilder()?,
- dynamic monthCellBuilder()?,
- dynamic yearCellBuilder()?,
- dynamic onViewStateChanged(
- ViewState viewState
- required dynamic onSelectedDateChanged(
- DateTime date
A customizable date picker widget that allows users to select dates, months, or years.
The CreateDatePicker widget provides a flexible and highly customizable date picker
with support for restricted dates, minimum and maximum date constraints, and custom
builders for various UI components.
Features:
- Switch between date, month, and year views.
- Restrict selectable dates using
minDateTime,maxDateTime, andrestrictedDates. - Customize the appearance of the picker using builder functions.
- Localize week and month labels using
weekLabelsandmonthLabels.
Example Usage:
CreateDatePicker(
width: 450,
initialViewState: ViewState.date,
initialDate: DateTime(2027, 6, 2),
minDateTime: DateTime(2024, 5, 1),
maxDateTime: DateTime(2029, 5, 1),
restrictedDates: restrictedDates,
weekLabels: weekLabels,
monthLabels: monthLabels,
leftArrowBuilder: _leftArrowBuilder,
rightArrowBuilder: _rightArrowBuilder,
dateViewButtonBuilder: _dateViewButtonBuilder,
monthViewButtonBuilder: _monthViewButtonBuilder,
yearViewButtonBuilder: _yearViewButtonBuilder,
weekdayCellBuilder: _weekdayCellBuilder,
dateCellBuilder: _dateCellBuilder,
monthCellBuilder: _monthCellBuilder,
yearCellBuilder: _yearCellBuilder,
onViewStateChanged: _changeViewState,
onSelectedDateChanged: _onSelectedDateChanged,
builder: _createDatePickerBuilder,
),
Notes:
- Ensure that
weekLabelscontains exactly 7 items. - Ensure that
monthLabelscontains exactly 12 items. - The
initialDatemust not be beforeminDateTimeor aftermaxDateTime. - The
initialDatemust not be included inrestrictedDates.
Implementation
CreateDatePicker({
super.key,
this.initialViewState = ViewState.date,
this.initialDate,
this.minDateTime,
this.maxDateTime,
this.restrictedDates = const [],
this.weekLabels = const [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
],
this.monthLabels = const [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
],
this.width,
this.elevation = 1,
this.popupMenuOffset,
this.popupMenuPosition = PopupMenuPosition.under,
this.builder,
this.leftArrowBuilder,
this.rightArrowBuilder,
this.popupSelectedDateBuilder,
this.dateViewButtonBuilder,
this.monthViewButtonBuilder,
this.yearViewButtonBuilder,
this.weekdayCellBuilder,
this.dateCellBuilder,
this.monthCellBuilder,
this.yearCellBuilder,
this.onViewStateChanged,
required this.onSelectedDateChanged,
}) : assert(
initialDate == null ||
(minDateTime == null || !initialDate.isBefore(minDateTime)),
'initialDate cannot be before minDateTime',
),
assert(
initialDate == null ||
(maxDateTime == null || !initialDate.isAfter(maxDateTime)),
'initialDate cannot be after maxDateTime',
),
assert(
initialDate == null ||
!restrictedDates.any((restrictedDate) {
return initialDate.year == restrictedDate.year &&
initialDate.month == restrictedDate.month &&
initialDate.day == restrictedDate.day;
}),
'initialDate cannot be included in restricted dates',
),
assert(
!restrictedDates.any((restrictedDate) {
return DateTime.now().year == restrictedDate.year &&
DateTime.now().month == restrictedDate.month &&
DateTime.now().day == restrictedDate.day;
}),
'initialDate cannot be included in restricted dates',
),
assert((weekLabels.length == 7), 'weekLabels must contain 7 days'),
assert((monthLabels.length == 12), 'monthLabels must contain 12 month');