Skip to content

feat(cupertino): Add weekType parameter to CupertinoDatePicker for selectable day control #171332 #171334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

koukibadr
Copy link
Contributor

@koukibadr koukibadr commented Jun 28, 2025

Description

This PR enhances the CupertinoDatePicker by introducing a weekType parameter, giving developers more control over which days users can select.

Solution

To address this, I've added a new weekType parameter to the CupertinoDatePicker widget. This parameter offers a declarative way to control the set of selectable days displayed in the picker, with options defined in a new WeekType enum:

WeekType.workDays (Monday-Friday)

WeekType.weekends (Saturday-Sunday)

WeekType.fullWeek (all days, current default behavior)

WeekType.custom (allows developers to provide a List representing the DateTime.weekday values that should be visible and selectable).

Example

// Example: Display only work days (Monday-Friday)
CupertinoDatePicker(
  mode: CupertinoDatePickerMode.date,
  initialDateTime: DateTime.now(),
  minimumDate: DateTime(2023, 1, 1),
  maximumDate: DateTime(2024, 12, 31),
  weekType: WeekType.workDays,
  onDateTimeChanged: (DateTime newDateTime) {
    // Handle date change
  },
);

Screenshots

workdays_display.mov
weekend_display.mov
custom_weekdays.mov

Here's the linked issue to this PR #171332

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: cupertino flutter/packages/flutter/cupertino repository labels Jun 28, 2025
@alex-medinsh
Copy link
Contributor

Hi @koukibadr! I think the API can be simplified by removing the weekType parameter and enum altogether and allowing the developer to just specify customWeekDays? It will then default to all 7 days and we can then have static presets, something like:

  static const List<int> weekends = [
    DateTime.saturday, 
    DateTime.sunday
  ];

  static const List<int> workDays = [
    DateTime.monday,
    DateTime.tuesday,
    DateTime.wednesday,
    DateTime.thursday,
    DateTime.friday,
  ];

It seems to me that it will help simplify the implementation as well. What do you think?

@koukibadr
Copy link
Contributor Author

Hi @koukibadr! I think the API can be simplified by removing the weekType parameter and enum altogether and allowing the developer to just specify customWeekDays? It will then default to all 7 days and we can then have static presets, something like:

  static const List<int> weekends = [
    DateTime.saturday, 
    DateTime.sunday
  ];

  static const List<int> workDays = [
    DateTime.monday,
    DateTime.tuesday,
    DateTime.wednesday,
    DateTime.thursday,
    DateTime.friday,
  ];

It seems to me that it will help simplify the implementation as well. What do you think?

@alex-medinsh yes it would be more cleaner I agree, but how developers will use presets ? it should be part of the CupertinoDatePIcker class right ?

@victorsanni
Copy link
Contributor

Hi @koukibadr! I think the API can be simplified by removing the weekType parameter and enum altogether and allowing the developer to just specify customWeekDays? It will then default to all 7 days and we can then have static presets, something like:

Talking with @MitchellGoodwin offline we think an even more general API can solve this problem and provide more control to developers. For example, a bool callback with a DateTime date arg that returns true if the date should be enabled or not. @alex-medinsh @koukibadr what do you think?

@alex-medinsh
Copy link
Contributor

alex-medinsh commented Jun 30, 2025

it should be part of the CupertinoDatePIcker class right ?

@koukibadr Initially I was thinking yes, but I am not sure how clean that is.

For example, a bool callback with a DateTime date arg that returns true if the date should be enabled or not.

@victorsanni Does the callback get the currently selected value as the argument? I think this can a bit strange in terms of the UX, since user can select any date and only then will it evaluate if it is selectable. Or did I misunderstand what you were suggesting?

What I think this PR is trying to accomplish is something akin to minimumDate and maximumDate, but with weekdays, where the user doesn't even see the disabled options.

Nevermind. The callback seems like a nice solution. :)

@MitchellGoodwin
Copy link
Contributor

MitchellGoodwin commented Jun 30, 2025

The Material date picker has a similar way of validating dates through selectableDayPredicate. A callback is a bit more flexible compared to a list when the list has to be made dynamic anyway through things like valid dates depend on outside logic or localization.

how developers will use presets ? it should be part of the CupertinoDatePIcker class right ?
Theoretically an enum of default callbacks could be provided, but I worry that it would be difficult to iterate on this enum. There's also localizations concerns. For instance "workDays" isn't valid for a lot of people, if the targets of the app work weekends, or it's in a culture that has a different work week, it loses relevance quickly.

@koukibadr
Copy link
Contributor Author

The Material date picker has a similar way of validating dates through selectableDayPredicate. A callback is a bit more flexible compared to a list when the list has to be made dynamic anyway through things like valid dates depend on outside logic or localization.

how developers will use presets ? it should be part of the CupertinoDatePIcker class right ?
Theoretically an enum of default callbacks could be provided, but I worry that it would be difficult to iterate on this enum. There's also localizations concerns. For instance "workDays" isn't valid for a lot of people, if the targets of the app work weekends, or it's in a culture that has a different work week, it loses relevance quickly.

@victorsanni @alex-medinsh @MitchellGoodwin Okay it make sense especially following the same logic as material design date picker, I'll update the implementation then
thanks for these clarifications

@@ -53,6 +53,9 @@ const double _kTimerPickerLabelFontSize = 17.0;
// The width of each column of the countdown time picker.
const double _kTimerPickerColumnIntrinsicWidth = 106;

/// Signature for predicating dates for enabled date selections.
typedef SelectableDayPredicate = bool Function(DateTime date);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This typedef is already defined in material/date.dart. Can we move some logic in that file to widgets/ and share it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that date.dart is related to specific Material DatePickers but yes we can refactor to have only one SelectableDayPredicate makes more sense

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koukibadr Do you plan to make this refactor? Just want to make sure we're on the same page. (from triage)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @dkwingsmt yes I'm back working on this just need to verify other Material DateTime features to not break anything with this refactor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Take your time!

@github-actions github-actions bot added the f: material design flutter/packages/flutter/material repository. label Jul 25, 2025
Copy link
Contributor

@victorsanni victorsanni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @koukibadr. Can you resolve the merge conflicts, rebase, and change the title of the PR to match the latest changes?

@koukibadr
Copy link
Contributor Author

@victorsanni
Okay I'll update my branch then

@fluttergithubbot
Copy link
Contributor

An existing Git SHA, 1590543f6794addbb7dff8c6d655a7546eb955aa, was detected, and no actions were taken.

To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with --force) that already was pushed before, push a blank commit (git commit --allow-empty -m "Trigger Build") or rebase to continue.

@koukibadr koukibadr reopened this Aug 9, 2025
@fluttergithubbot
Copy link
Contributor

An existing Git SHA, 301e896dbd6dd4ddeb68f7da99cde5ccb830906f, was detected, and no actions were taken.

To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with --force) that already was pushed before, push a blank commit (git commit --allow-empty -m "Trigger Build") or rebase to continue.

@koukibadr
Copy link
Contributor Author

@victorsanni I've resolved the conflict and rebased latest master branch

@koukibadr koukibadr requested a review from victorsanni August 9, 2025 08:46
@victorsanni
Copy link
Contributor

Hi @koukibadr, can you run dart format packages/flutter/lib/src/cupertino/date_picker.dart to fix the failing Linux analyze test? The other failing test is a bot timeout which should be corrected once you push the formatting changes.

@koukibadr koukibadr requested a review from victorsanni August 17, 2025 14:48
@koukibadr koukibadr requested a review from victorsanni August 18, 2025 21:25
),
assert(
selectableDayPredicate == null ||
selectableDayPredicate(initialDateTime ?? DateTime.now()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure DateTime.now() should be used here. What about

selectableDayPredicate == null || initialDate == null || selectableDayPredicate!(this.initialDate!)

like in DatePicker?

Comment on lines 885 to 890
final bool isValidDate;
if (widget.selectableDayPredicate == null) {
isValidDate = true;
} else {
isValidDate = widget.selectableDayPredicate!.call(rangeStart);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with a method:

  bool _isSelectable(DateTime date) {
    return widget.selectableDayPredicate?.call(date) ?? true;
  }

and call it with rangeStart:

return itemPositioningBuilder(context, Text(dateText, style: _themeTextStyle(context)));
return itemPositioningBuilder(
context,
Text(dateText, style: _themeTextStyle(context, isValid: isValidDate)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Text(dateText, style: _themeTextStyle(context, isValid: isValidDate)),
Text(dateText, style: _themeTextStyle(context, isValid: _isSelectable(rangeStart)),

final DateTime selectedDate = selectedDateTime;
final bool minCheck = widget.minimumDate?.isAfter(selectedDate) ?? false;

if (widget.selectableDayPredicate?.call(selectedDate) == false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_isSelectable can be used here as well. If needed, the function can be defined outside of any classes and pass in widget.selectableDayPredicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants