-
Notifications
You must be signed in to change notification settings - Fork 29.1k
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
base: master
Are you sure you want to change the base?
Conversation
Hi @koukibadr! I think the API can be simplified by removing the 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 ? |
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 |
@koukibadr Initially I was thinking yes, but I am not sure how clean that is.
Nevermind. The callback seems like a nice solution. :) |
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.
|
@victorsanni @alex-medinsh @MitchellGoodwin Okay it make sense especially following the same logic as material design date picker, I'll update the implementation then |
@@ -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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome. Take your time!
There was a problem hiding this 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?
@victorsanni |
An existing Git SHA, To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with |
An existing Git SHA, To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with |
@victorsanni I've resolved the conflict and rebased latest master branch |
Hi @koukibadr, can you run |
… in Cupertino DatePicker
), | ||
assert( | ||
selectableDayPredicate == null || | ||
selectableDayPredicate(initialDateTime ?? DateTime.now()), |
There was a problem hiding this comment.
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
?
final bool isValidDate; | ||
if (widget.selectableDayPredicate == null) { | ||
isValidDate = true; | ||
} else { | ||
isValidDate = widget.selectableDayPredicate!.call(rangeStart); | ||
} |
There was a problem hiding this comment.
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)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) { |
There was a problem hiding this comment.
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
.
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
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.