-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Update CupertinoSwitch thumb to snap to the sides on drag. #176825
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
Update CupertinoSwitch thumb to snap to the sides on drag. #176825
Conversation
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.
Code Review
This pull request updates the CupertinoSwitch to snap to the sides during a drag and adjusts the haptic feedback to trigger only when the thumb crosses the midpoint. The implementation looks correct and aligns with the behavior shown in the videos. The addition of a new test for the snapping behavior is great. However, I've found a few issues in the existing tests that were modified, mainly around brittle assertions that check for exact animation values during a transition. I've also suggested a simplification for a test helper to improve clarity and correctness. Overall, good changes to improve the user experience of the switch.
7403fe6 to
666b05e
Compare
victorsanni
left a comment
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.
Thanks for this fix. I was playing around with the native switch, and I see that the switch visually toggled when dragged ~75% of the track length:
native.switch.mov
In this PR, the switch visually toggles when dragged 50% of the track length:
pr.switch.mov
Can you add a video comparing with native side-by-side in the PR description?
|
Hi @victorsanni, thank you for taking a look at this PR and providing such valuable information regarding the native iOS switch behavior. I've investigated how the UISwitch behaves and updated this PR accordingly. The following screen recording compares the updated switch_comparison.movAs the video demonstrates, the new logic better matches the native behavior:
The latest change adds the constants The comparison demo repository is available here: https://github.com/ksokolovskyi/cupertino_switch_comparison |
|
LGTM. Requesting a review from others who have worked on |
MitchellGoodwin
left a comment
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.
LGTM as well!
| : _kDragCommitThreshold; | ||
| final double effectiveThreshold = widget.value ? -threshold : threshold; | ||
|
|
||
| final bool currentDragValue = _dragDelta >= effectiveThreshold; |
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.
Nit: I think this gets a little confusing on which "drag value" means what at a glance. currentDragValue may be better named to something like "newDragValue".
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.
Thanks a lot for the feedback! I've renamed it in the latest commit.
…76825) Fixes flutter#166485 ## Description - Updates `CupertinoSwitch` thumb to snap to the sides on drag - Updates `CupertinoSwitch` to emit vibration not when drag starts, but when the dragged thumb crosses the middle point | BEFORE | AFTER | | - | - | | <video alt="before" src="https://github.com/user-attachments/assets/467f0a3c-ab6a-40c0-a0fe-6b1ff835bbb0" /> | <video alt="after" src="https://github.com/user-attachments/assets/9bc21bdc-e8a3-4626-8155-1d90614a72fa" /> | ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victor Sanni <[email protected]>
…76825) Fixes flutter#166485 ## Description - Updates `CupertinoSwitch` thumb to snap to the sides on drag - Updates `CupertinoSwitch` to emit vibration not when drag starts, but when the dragged thumb crosses the middle point | BEFORE | AFTER | | - | - | | <video alt="before" src="https://github.com/user-attachments/assets/467f0a3c-ab6a-40c0-a0fe-6b1ff835bbb0" /> | <video alt="after" src="https://github.com/user-attachments/assets/9bc21bdc-e8a3-4626-8155-1d90614a72fa" /> | ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victor Sanni <[email protected]>
…76825) Fixes flutter#166485 ## Description - Updates `CupertinoSwitch` thumb to snap to the sides on drag - Updates `CupertinoSwitch` to emit vibration not when drag starts, but when the dragged thumb crosses the middle point | BEFORE | AFTER | | - | - | | <video alt="before" src="https://github.com/user-attachments/assets/467f0a3c-ab6a-40c0-a0fe-6b1ff835bbb0" /> | <video alt="after" src="https://github.com/user-attachments/assets/9bc21bdc-e8a3-4626-8155-1d90614a72fa" /> | ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victor Sanni <[email protected]>
Fixes #166485
Description
CupertinoSwitchthumb to snap to the sides on dragCupertinoSwitchto emit vibration not when drag starts, but when the dragged thumb crosses the middle pointbefore.mov
after.mov
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.