Skip to content

Commit dbd277b

Browse files
committed
Simplify discrete-rounded-track padding logic per review feedback
Extracts the repeated isDiscrete/isRounded padding calculation into a single _discreteRoundedTrackPadding getter and flattens the branching in _getValueFromGlobalPosition, per dkwingsmt's review suggestion.
1 parent 82cd056 commit dbd277b

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

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

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,13 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
12761276
isDiscrete: false,
12771277
);
12781278

1279+
// Tick marks and the thumb are painted in an adjusted coordinate space that
1280+
// reserves half a track height of padding on each end for discrete rounded
1281+
// tracks, so gesture math needs to account for that same padding to stay
1282+
// aligned with the visual tick positions.
1283+
double get _discreteRoundedTrackPadding =>
1284+
(isDiscrete && _sliderTheme.trackShape!.isRounded) ? _trackRect.height : 0.0;
1285+
12791286
bool get isInteractive => onChanged != null;
12801287

12811288
bool get isDiscrete => divisions != null && divisions! > 0;
@@ -1581,25 +1588,15 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
15811588

15821589
double _getValueFromGlobalPosition(Offset globalPosition) {
15831590
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-
}
1591+
final double padding = _discreteRoundedTrackPadding;
1592+
final double adjustedWidth = _trackRect.width - padding;
1593+
// Invert the padded track-geometry formula (see _discreteRoundedTrackPadding)
1594+
// so tap coordinates align with the visual tick positions. Without this, the
1595+
// snap midpoints in tap space diverge from the visual midpoints between tick
1596+
// marks, biasing lower-half taps toward the wrong (lower) division.
1597+
final double visualPosition = adjustedWidth <= 0.0
1598+
? 0.5
1599+
: (localDx - _trackRect.left - padding / 2) / adjustedWidth;
16031600
return _getValueFromVisualPosition(visualPosition);
16041601
}
16051602

@@ -1686,9 +1683,7 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
16861683
if (_active && isInteractive) {
16871684
// Use the same coordinate width as _getValueFromGlobalPosition so
16881685
// 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;
1686+
final double effectiveWidth = _trackRect.width - _discreteRoundedTrackPadding;
16921687
if (effectiveWidth <= 0.0) {
16931688
break;
16941689
}

0 commit comments

Comments
 (0)