Repository navigation
[material_ui] Fix discrete Slider selecting the neighboring tick near the track ends #12927
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
Merged
auto-submit
merged 4 commits into
flutter:main
from
hkarmoush:fix/184391-slider-discrete-tap-wrong-value
Nov 3, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e282a1b
Fix discrete Slider snapping to wrong value when tapping lower-half t…
hkarmoush 7de8648
Make discrete slider regression tests fail without the fix
hkarmoush ae4d3a1
[material_ui] Update changelog to reference the new issue
hkarmoush 222d3a0
Update packages/material_ui/pending_changelogs/change_2026_09_18_disc…
QuncCccccc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1303,6 +1303,13 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin { | |
| isDiscrete: false, | ||
| ); | ||
|
|
||
| // Tick marks and the thumb are painted in an adjusted coordinate space that | ||
| // reserves half a track height of padding on each end for discrete rounded | ||
| // tracks, so gesture math needs to account for that same padding to stay | ||
| // aligned with the visual tick positions. | ||
| double get _discreteRoundedTrackPadding => | ||
| (isDiscrete && _sliderTheme.trackShape!.isRounded) ? _trackRect.height : 0.0; | ||
|
Comment on lines
+1310
to
+1311
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the null-assert operator ( double get _discreteRoundedTrackPadding =>
(isDiscrete && (_sliderTheme.trackShape?.isRounded ?? false)) ? _trackRect.height : 0.0; |
||
|
|
||
| bool get isInteractive => onChanged != null; | ||
|
|
||
| bool get isDiscrete => divisions != null && divisions! > 0; | ||
|
|
@@ -1620,8 +1627,16 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin { | |
| } | ||
|
|
||
| double _getValueFromGlobalPosition(Offset globalPosition) { | ||
| final double visualPosition = | ||
| (globalToLocal(globalPosition).dx - _trackRect.left) / _trackRect.width; | ||
| final double localDx = globalToLocal(globalPosition).dx; | ||
| final double padding = _discreteRoundedTrackPadding; | ||
| final double adjustedWidth = _trackRect.width - padding; | ||
| // Invert the padded track-geometry formula (see _discreteRoundedTrackPadding) | ||
| // so tap coordinates align with the visual tick positions. Without this, the | ||
| // snap midpoints in tap space diverge from the visual midpoints between tick | ||
| // marks, biasing lower-half taps toward the wrong (lower) division. | ||
| final double visualPosition = adjustedWidth <= 0.0 | ||
| ? 0.5 | ||
| : (localDx - _trackRect.left - padding / 2) / adjustedWidth; | ||
| return _getValueFromVisualPosition(visualPosition); | ||
| } | ||
|
|
||
|
|
@@ -1704,7 +1719,13 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin { | |
| case SliderInteraction.slideOnly: | ||
| case SliderInteraction.slideThumb: | ||
| if (_active && isInteractive) { | ||
| final double valueDelta = details.primaryDelta! / _trackRect.width; | ||
| // Use the same coordinate width as _getValueFromGlobalPosition so | ||
| // that dragging immediately after a tap does not jump. | ||
| final double effectiveWidth = _trackRect.width - _discreteRoundedTrackPadding; | ||
| if (effectiveWidth <= 0.0) { | ||
| break; | ||
| } | ||
| final double valueDelta = details.primaryDelta! / effectiveWidth; | ||
| _currentDragValue += switch (textDirection) { | ||
| TextDirection.rtl => -valueDelta, | ||
| TextDirection.ltr => valueDelta, | ||
|
|
||
3 changes: 3 additions & 0 deletions
3
...ges/material_ui/pending_changelogs/change_2026_09_18_discrete_slider_tap_wrong_value.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| changelog: | | ||
| - Fixes discrete `Slider` and `RangeSlider` with a rounded track selecting the neighboring division when tapping ticks near the ends of the track. | ||
| version: patch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Using the null-assert operator (
!) on_sliderTheme.rangeTrackShapecan lead to a runtime crash if the range track shape is null in the theme. It is safer to use optional chaining (?.) with a fallback value to ensure robustness. Additionally, handling the case where_trackRect.widthis<= 0.0in theelsebranch prevents potential division-by-zero errors.