mirror of
https://github.com/flutter/flutter.git
synced 2026-02-11 05:17:36 +08:00
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 - [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. - [x] 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.