Skip to content
10 changes: 10 additions & 0 deletions lib/src/models/calendar_date_picker2_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ class CalendarDatePicker2Config {
this.dayModeScrollDirection,
this.selectedRangeHighlightBuilder,
this.selectedRangeDecorationPredicate,
this.isPickMonthMode,
}) : calendarType = calendarType ?? CalendarDatePicker2Type.single,
firstDate = DateUtils.dateOnly(firstDate ?? DateTime(1970)),
lastDate =
Expand Down Expand Up @@ -431,6 +432,9 @@ class CalendarDatePicker2Config {
/// Predicate to determine the day widget box decoration for a day in selected range
final SelectedRangeDecorationPredicate? selectedRangeDecorationPredicate;

/// Flag to auto transition to day view when the user selects a month
final bool? isPickMonthMode;

/// Copy the current [CalendarDatePicker2Config] with some new values
CalendarDatePicker2Config copyWith({
CalendarDatePicker2Type? calendarType,
Expand Down Expand Up @@ -504,6 +508,7 @@ class CalendarDatePicker2Config {
Axis? dayModeScrollDirection,
SelectedRangeHighlightBuilder? selectedRangeHighlightBuilder,
SelectedRangeDecorationPredicate? selectedRangeDecorationPredicate,
bool? isPickMonthMode,
}) {
return CalendarDatePicker2Config(
calendarType: calendarType ?? this.calendarType,
Expand Down Expand Up @@ -607,6 +612,7 @@ class CalendarDatePicker2Config {
selectedRangeHighlightBuilder ?? this.selectedRangeHighlightBuilder,
selectedRangeDecorationPredicate: selectedRangeDecorationPredicate ??
this.selectedRangeDecorationPredicate,
isPickMonthMode: isPickMonthMode ?? this.isPickMonthMode,
);
}
}
Expand Down Expand Up @@ -695,6 +701,7 @@ class CalendarDatePicker2WithActionButtonsConfig
this.closeDialogOnCancelTapped,
this.closeDialogOnOkTapped,
this.buttonPadding,
bool? isPickMonthMode,
}) : super(
calendarType: calendarType,
firstDate: firstDate,
Expand Down Expand Up @@ -767,6 +774,7 @@ class CalendarDatePicker2WithActionButtonsConfig
dayModeScrollDirection: dayModeScrollDirection,
selectedRangeHighlightBuilder: selectedRangeHighlightBuilder,
selectedRangeDecorationPredicate: selectedRangeDecorationPredicate,
isPickMonthMode: isPickMonthMode,
);

/// The gap between calendar and action buttons
Expand Down Expand Up @@ -878,6 +886,7 @@ class CalendarDatePicker2WithActionButtonsConfig
Axis? dayModeScrollDirection,
SelectedRangeHighlightBuilder? selectedRangeHighlightBuilder,
SelectedRangeDecorationPredicate? selectedRangeDecorationPredicate,
bool? isPickMonthMode,
}) {
return CalendarDatePicker2WithActionButtonsConfig(
calendarType: calendarType ?? this.calendarType,
Expand Down Expand Up @@ -994,6 +1003,7 @@ class CalendarDatePicker2WithActionButtonsConfig
selectedRangeHighlightBuilder ?? this.selectedRangeHighlightBuilder,
selectedRangeDecorationPredicate: selectedRangeDecorationPredicate ??
this.selectedRangeDecorationPredicate,
isPickMonthMode: isPickMonthMode,
);
}
}
23 changes: 20 additions & 3 deletions lib/src/widgets/calendar_date_picker2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,19 @@ class _CalendarDatePicker2State extends State<CalendarDatePicker2> {

void _handleMonthChanged(DateTime value) {
_vibrate();

setState(() {
_mode = CalendarDatePicker2Mode.day;
if (widget.config.isPickMonthMode != true) {
_mode = CalendarDatePicker2Mode.day;
}

_handleDisplayedMonthDateChanged(value);

_selectedDates.removeLast();
_selectedDates.add(_currentDisplayedMonthDate);
});

widget.onValueChanged?.call(_selectedDates.whereType<DateTime>().toList());
}

void _handleYearChanged(DateTime value) {
Expand All @@ -244,9 +253,17 @@ class _CalendarDatePicker2State extends State<CalendarDatePicker2> {
}

setState(() {
_mode = CalendarDatePicker2Mode.day;
if (widget.config.calendarViewMode == CalendarDatePicker2Mode.month || widget.config.calendarViewMode == CalendarDatePicker2Mode.year) {
_mode = CalendarDatePicker2Mode.month;
} else {
_mode = CalendarDatePicker2Mode.day;
}
_handleDisplayedMonthDateChanged(value, fromYearPicker: true);
});

_selectedDates.removeLast();
_selectedDates.add(value);
widget.onValueChanged?.call(_selectedDates.whereType<DateTime>().toList());
}

void _handleDayChanged(DateTime value) {
Expand Down Expand Up @@ -393,7 +410,7 @@ class _CalendarDatePicker2State extends State<CalendarDatePicker2> {
mode: _mode,
monthDate: _currentDisplayedMonthDate,
onMonthPressed: () {
if (_mode == CalendarDatePicker2Mode.year) {
if (_mode == CalendarDatePicker2Mode.year || widget.config.isPickMonthMode == true) {
_handleModeChanged(CalendarDatePicker2Mode.month);
} else {
_handleModeChanged(
Expand Down