Skip to content

Commit 7f2957f

Browse files
committed
[material_ui] Fix floating-point rounding errors for discrete Slider and RangeSlider
Fixes floating-point rounding errors when calculating values for discrete `Slider` and `RangeSlider`. Ports flutter/flutter#187393 to `material_ui`. Fixes flutter/flutter#39510
1 parent 897192a commit 7f2957f

5 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎packages/material_ui/lib/src/range_slider.dart‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,13 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
727727

728728
// Returns a number between min and max, proportional to value, which must
729729
// be between 0.0 and 1.0.
730-
double _lerp(double value) => ui.lerpDouble(widget.min, widget.max, value)!;
730+
double _lerp(double value) {
731+
final int? divisions = widget.divisions;
732+
if (divisions != null) {
733+
return widget.min + (value * divisions).round() * (widget.max - widget.min) / divisions;
734+
}
735+
return ui.lerpDouble(widget.min, widget.max, value)!;
736+
}
731737

732738
// Returns a new range value with the start and end lerped.
733739
RangeValues _lerpRangeValues(RangeValues values) {

‎packages/material_ui/lib/src/slider.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,10 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
804804
double _lerp(double value) {
805805
assert(value >= 0.0);
806806
assert(value <= 1.0);
807+
final int? divisions = widget.divisions;
808+
if (divisions != null) {
809+
return widget.min + (value * divisions).round() * (widget.max - widget.min) / divisions;
810+
}
807811
return value * (widget.max - widget.min) + widget.min;
808812
}
809813

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes floating-point rounding errors when calculating values for discrete `Slider` and `RangeSlider`.
3+
version: patch

‎packages/material_ui/test/range_slider_test.dart‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,49 @@ void main() {
837837
},
838838
);
839839

840+
testWidgets('Discrete range slider has no floating-point rounding errors', (
841+
WidgetTester tester,
842+
) async {
843+
final valuesList = <RangeValues>[];
844+
await tester.pumpWidget(
845+
MaterialApp(
846+
home: Directionality(
847+
textDirection: TextDirection.ltr,
848+
child: Material(
849+
child: Center(
850+
child: SizedBox(
851+
width: 200.0,
852+
child: RangeSlider(
853+
max: 35.0,
854+
divisions: 35,
855+
values: const RangeValues(0.0, 35.0),
856+
onChanged: (RangeValues newValues) {
857+
valuesList.add(newValues);
858+
},
859+
),
860+
),
861+
),
862+
),
863+
),
864+
),
865+
);
866+
867+
final Offset topLeft = tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0);
868+
final Offset bottomRight = tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0);
869+
final double activeTrackWidth = bottomRight.dx - topLeft.dx;
870+
871+
final startThumb = topLeft;
872+
final TestGesture gesture = await tester.startGesture(startThumb);
873+
await gesture.moveTo(topLeft + Offset(activeTrackWidth * 15.0 / 35.0, 0.0));
874+
await gesture.up();
875+
876+
expect(valuesList.isNotEmpty, isTrue);
877+
for (final val in valuesList) {
878+
expect(val.start, equals(val.start.roundToDouble()));
879+
expect(val.end, equals(val.end.roundToDouble()));
880+
}
881+
});
882+
840883
testWidgets(
841884
'Range Slider thumbs can be dragged together and the end thumb can be dragged apart (continuous LTR)',
842885
(WidgetTester tester) async {

‎packages/material_ui/test/slider_test.dart‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,46 @@ void main() {
679679
expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
680680
});
681681

682+
testWidgets('Discrete slider has no floating-point rounding errors', (WidgetTester tester) async {
683+
final values = <double>[];
684+
await tester.pumpWidget(
685+
MaterialApp(
686+
home: Directionality(
687+
textDirection: TextDirection.ltr,
688+
child: Material(
689+
child: Center(
690+
child: SizedBox(
691+
width: 180.0,
692+
child: Slider(
693+
max: 35.0,
694+
divisions: 35,
695+
value: 0.0,
696+
onChanged: (double newValue) {
697+
values.add(newValue);
698+
},
699+
),
700+
),
701+
),
702+
),
703+
),
704+
),
705+
);
706+
707+
final Offset topLeft = tester.getTopLeft(find.byType(Slider));
708+
final Offset bottomRight = tester.getBottomRight(find.byType(Slider));
709+
final double width = bottomRight.dx - topLeft.dx;
710+
final double activeTrackWidth = width - 48.0;
711+
final Offset start = topLeft + Offset(24.0, (bottomRight.dy - topLeft.dy) / 2);
712+
final TestGesture gesture = await tester.startGesture(start);
713+
await gesture.moveTo(start + Offset(activeTrackWidth * 29.0 / 35.0, 0.0));
714+
await gesture.up();
715+
716+
expect(values.isNotEmpty, isTrue);
717+
for (final val in values) {
718+
expect(val, equals(val.roundToDouble()));
719+
}
720+
});
721+
682722
testWidgets('Slider can be given zero values', (WidgetTester tester) async {
683723
final log = <double>[];
684724
await tester.pumpWidget(

0 commit comments

Comments
 (0)