Skip to content

Commit 2768ebe

Browse files
hkarmoushFarisArmoush
authored andcommitted
Fix discrete Slider snapping to wrong value when tapping lower-half tick marks
Fixes flutter#184391 Two bugs caused discrete Slider taps to land on the wrong division: 1. Coordinate system mismatch: tick marks and the thumb are painted at `trackLeft + i/d * (trackWidth - padding) + padding/2`, but tap detection inverted a different, unpadded formula. On rounded tracks (RoundedRectSliderTrackShape, GappedSliderTrackShape) this pushes lower-half tap midpoints into the wrong snap bucket. The mismatch is 4 px on M2 and up to 16 px on M3. Fix: _getValueFromGlobalPosition now uses the same padded formula when isDiscrete && trackShape.isRounded. The drag-delta normalizer in _handleDragUpdate is updated to match so tap-then-drag is consistent. 2. IEEE 754 precision: 0.35 * 10 = 3.4999…9 in double arithmetic, so .round() returns 3 instead of 4 at the midpoint between ticks 3 and 4. A 1e-10 epsilon guard in _discretize corrects this without biasing values that are genuinely below the midpoint. Both fixes are applied to Slider and RangeSlider. Five regression tests are added to slider_test.dart covering M2 LTR, M3 LTR, RTL, drag continuity, and the IEEE 754 midpoint case.
1 parent cf9e8af commit 2768ebe

3 files changed

Lines changed: 349 additions & 8 deletions

File tree

‎packages/flutter/lib/src/material/range_slider.dart‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,15 +1320,30 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
13201320
}
13211321

13221322
double _getValueFromGlobalPosition(Offset globalPosition) {
1323-
final double visualPosition =
1324-
(globalToLocal(globalPosition).dx - _trackRect.left) / _trackRect.width;
1323+
final double localDx = globalToLocal(globalPosition).dx;
1324+
final double visualPosition;
1325+
// Mirror the padded coordinate system used when painting tick marks and
1326+
// thumbs on discrete rounded tracks (see _RenderSlider._getValueFromGlobalPosition).
1327+
if (isDiscrete && _sliderTheme.rangeTrackShape!.isRounded) {
1328+
final double padding = _trackRect.height;
1329+
final double adjustedWidth = _trackRect.width - padding;
1330+
if (adjustedWidth <= 0.0) {
1331+
visualPosition = 0.5;
1332+
} else {
1333+
visualPosition = (localDx - _trackRect.left - padding / 2) / adjustedWidth;
1334+
}
1335+
} else {
1336+
visualPosition = (localDx - _trackRect.left) / _trackRect.width;
1337+
}
13251338
return _getValueFromVisualPosition(visualPosition);
13261339
}
13271340

13281341
double _discretize(double value) {
13291342
double result = clampDouble(value, 0.0, 1.0);
13301343
if (isDiscrete) {
1331-
result = (result * divisions!).round() / divisions!;
1344+
// Add a small epsilon before rounding to fix IEEE 754 representation
1345+
// errors where values like 0.35 * 10 produce 3.4999... instead of 3.5.
1346+
result = clampDouble(((result * divisions!) + 1e-10).round() / divisions!, 0.0, 1.0);
13321347
}
13331348
return result;
13341349
}

‎packages/flutter/lib/src/material/slider.dart‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,10 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
784784
assert(value >= 0.0 && value <= 1.0);
785785

786786
final int divisions = widget.divisions!;
787-
return (value * divisions).round() / divisions;
787+
// Add a small epsilon before rounding to fix IEEE 754 representation errors
788+
// where values like 0.35 * 10 produce 3.4999... instead of 3.5, causing
789+
// them to round to the wrong (lower) division.
790+
return clampDouble(((value * divisions) + 1e-10).round() / divisions, 0.0, 1.0);
788791
}
789792

790793
double _convert(double value) {
@@ -1577,15 +1580,35 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
15771580
}
15781581

15791582
double _getValueFromGlobalPosition(Offset globalPosition) {
1580-
final double visualPosition =
1581-
(globalToLocal(globalPosition).dx - _trackRect.left) / _trackRect.width;
1583+
final double localDx = globalToLocal(globalPosition).dx;
1584+
final double visualPosition;
1585+
// Tick marks and the thumb are painted in an adjusted coordinate space that
1586+
// accounts for track height padding on discrete rounded tracks:
1587+
// dx = trackLeft + value * (trackWidth - padding) + padding/2
1588+
// Invert that same formula here so tap coordinates align with the visual
1589+
// tick positions. Without this, the snap midpoints in tap space diverge
1590+
// from the visual midpoints between tick marks, biasing lower-half taps
1591+
// toward the wrong (lower) division.
1592+
if (isDiscrete && _sliderTheme.trackShape!.isRounded) {
1593+
final double padding = _trackRect.height;
1594+
final double adjustedWidth = _trackRect.width - padding;
1595+
if (adjustedWidth <= 0.0) {
1596+
visualPosition = 0.5;
1597+
} else {
1598+
visualPosition = (localDx - _trackRect.left - padding / 2) / adjustedWidth;
1599+
}
1600+
} else {
1601+
visualPosition = (localDx - _trackRect.left) / _trackRect.width;
1602+
}
15821603
return _getValueFromVisualPosition(visualPosition);
15831604
}
15841605

15851606
double _discretize(double value) {
15861607
double result = clampDouble(value, 0.0, 1.0);
15871608
if (isDiscrete) {
1588-
result = (result * divisions!).round() / divisions!;
1609+
// Add a small epsilon before rounding to fix IEEE 754 representation
1610+
// errors where values like 0.35 * 10 produce 3.4999... instead of 3.5.
1611+
result = clampDouble(((result * divisions!) + 1e-10).round() / divisions!, 0.0, 1.0);
15891612
}
15901613
return result;
15911614
}
@@ -1661,7 +1684,15 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
16611684
case SliderInteraction.slideOnly:
16621685
case SliderInteraction.slideThumb:
16631686
if (_active && isInteractive) {
1664-
final double valueDelta = details.primaryDelta! / _trackRect.width;
1687+
// Use the same coordinate width as _getValueFromGlobalPosition so
1688+
// that dragging immediately after a tap does not jump.
1689+
final double effectiveWidth = (isDiscrete && _sliderTheme.trackShape!.isRounded)
1690+
? _trackRect.width - _trackRect.height
1691+
: _trackRect.width;
1692+
if (effectiveWidth <= 0.0) {
1693+
break;
1694+
}
1695+
final double valueDelta = details.primaryDelta! / effectiveWidth;
16651696
_currentDragValue += switch (textDirection) {
16661697
TextDirection.rtl => -valueDelta,
16671698
TextDirection.ltr => valueDelta,

0 commit comments

Comments
 (0)