Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feat: Add momentary varient of CupertinoSlidingSegmentedControl #164262

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

rkishan516
Copy link
Contributor

@rkishan516 rkishan516 commented Feb 27, 2025

This PR introduces a new boolean flag, isMomentary, to the CupertinoSlidingSegmentedControl widget.

When isMomentary is set to true, the segmented control behaves like a momentary switch. Selecting a segment triggers its associated action (e.g., via onValueChanged), but the segment doesn't stay visually selected after the interaction ends. The control visually returns to its previous state or a default state (often no segment selected).

When isMomentary is false (the default behavior), the control maintains the selection state visually, highlighting the last chosen segment.

Motivation

Currently, CupertinoSlidingSegmentedControl always maintains a selected state. There are use cases where developers might want to use the segmented control purely for triggering actions without persisting a visual selection state, similar to how a UISegmentedControl can be configured on iOS with isMomentary = true. This provides more flexibility and aligns the Flutter widget closer to its native counterpart's capabilities.

For example, this could be useful for triggering sorting actions, view changes, or other immediate operations where maintaining a persistent "selected" state isn't desired or meaningful.

Related Issues
fixes: #164077

Videos
UISegmentedControl behaviour when isMomentary flag is set to true.
https://github.com/user-attachments/assets/84d00312-4530-4b7a-9696-142f60ad89c0

CupertinoSlidingSegmentedControl behaviour when isMomentary flag is set to true.
https://github.com/user-attachments/assets/b308b4fa-e641-417a-a518-4e0908876eed

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: cupertino flutter/packages/flutter/cupertino repository labels Feb 27, 2025
@rkishan516 rkishan516 force-pushed the sliding-segmented-control-momentary branch from 1d13bdb to 77e7ded Compare February 28, 2025 16:41
@rkishan516
Copy link
Contributor Author

@QuncCccccc Can you please review this PR ?

@QuncCccccc QuncCccccc self-requested a review March 12, 2025 03:51
Copy link
Contributor

@QuncCccccc QuncCccccc left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

I compared the native SegmentedControl behavior with the newly added behavior here, and they look quite different.

For native component, when momentary is turned on and we tap/press a segment, the text would animate a little and go back to the original size, and onValueChanged is still get called when we tap one of the segments.

Screen.Recording.2025-03-14.at.3.17.01.PM.mov

I'm not sure what would be the use case of momentary flag, but if we really need this feature, we might want to match the native behavior as much as possible:)

@rkishan516
Copy link
Contributor Author

rkishan516 commented Mar 14, 2025

Thanks for your contribution!

I compared the native SegmentedControl behavior with the newly added behavior here, and they look quite different.

For native component, when momentary is turned on and we tap/press a segment, the text would animate a little and go back to the original size, and onValueChanged is still get called when we tap one of the segments.

Screen.Recording.2025-03-14.at.3.17.01.PM.mov
I'm not sure what would be the use case of momentary flag, but if we really need this feature, we might want to match the native behavior as much as possible:)

Sure, I'll try to match as much as possible.

Edit :- Have allowed onValueChanged onTap and also Updated onTap animation.

@rkishan516 rkishan516 force-pushed the sliding-segmented-control-momentary branch 4 times, most recently from 01e0257 to df72eb9 Compare March 15, 2025 05:06
@rkishan516 rkishan516 requested a review from QuncCccccc March 15, 2025 05:09
@dkwingsmt dkwingsmt requested a review from Piinks April 2, 2025 18:27
Copy link
Contributor

@Piinks Piinks left a comment

Choose a reason for hiding this comment

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

Thanks for sending another PR! Can you add a dartpad sample to the docs that demonstrates this?

@@ -2126,4 +2126,30 @@ void main() {

expect(onValueChangedCalled, 0);
});

testWidgets('CupertinoSlidingSegmentedControl can be momentary', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

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

There should also be a test to verify the scale was applied correctly.
Also, looking at all the conditionals around this, are each tested?

Copy link
Contributor Author

@rkishan516 rkishan516 Apr 8, 2025

Choose a reason for hiding this comment

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

@Piinks All conditions are added such that it doesn't highlight any item. But yes I can add a test for scale.

@rkishan516
Copy link
Contributor Author

Thanks for sending another PR! Can you add a dartpad sample to the docs that demonstrates this?

Sure, I can add dartpad sample.

@rkishan516 rkishan516 force-pushed the sliding-segmented-control-momentary branch from df72eb9 to 9b72c30 Compare April 8, 2025 04:01
@github-actions github-actions bot added d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos labels Apr 8, 2025
@rkishan516 rkishan516 force-pushed the sliding-segmented-control-momentary branch 2 times, most recently from aee2513 to 748d8fa Compare April 10, 2025 01:57
@flutter-dashboard
Copy link

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@Piinks Piinks self-requested a review April 30, 2025 02:32
@Piinks
Copy link
Contributor

Piinks commented May 1, 2025

Hey @rkishan516, this is the type of change we want to make sure has high fidelity with the native feature. Can you update the PR description with visuals to demonstrate the native behavior and what you are adding here? I see some of the earlier feedback here found some issues when trying to compare.

In general, it's preferred to provide a more detailed explanation of your change in PR descriptions. Thank you!

@rkishan516
Copy link
Contributor Author

Hey @rkishan516, this is the type of change we want to make sure has high fidelity with the native feature. Can you update the PR description with visuals to demonstrate the native behavior and what you are adding here? I see some of the earlier feedback here found some issues when trying to compare.

In general, it's preferred to provide a more detailed explanation of your change in PR descriptions. Thank you!

Sure @Piinks, I will update description.

@rkishan516 rkishan516 force-pushed the sliding-segmented-control-momentary branch from 748d8fa to 4a376e5 Compare May 2, 2025 02:23
@rkishan516 rkishan516 force-pushed the sliding-segmented-control-momentary branch from 4a376e5 to 4b1a806 Compare May 2, 2025 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos f: cupertino flutter/packages/flutter/cupertino repository framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support isMomentary feature for CupertinoSlidingSegmentedControl
4 participants