Skip to content

Commit bcc6dcf

Browse files
authored
[material_ui] Fix floating-point rounding errors for discrete Slider and RangeSlider (flutter#13022)
Fixes floating-point rounding errors when calculating values for discrete `Slider` and `RangeSlider`. Ports flutter#187393 to `material_ui`. Fixes flutter#39510 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 6c76d46 commit bcc6dcf

5 files changed

Lines changed: 101 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,15 @@ 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+
assert(value >= 0.0);
732+
assert(value <= 1.0);
733+
final int? divisions = widget.divisions;
734+
if (divisions != null) {
735+
return widget.min + (value * divisions).round() * (widget.max - widget.min) / divisions;
736+
}
737+
return ui.lerpDouble(widget.min, widget.max, value)!;
738+
}
731739

732740
// Returns a new range value with the start and end lerped.
733741
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,50 @@ 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 * 29.0 / 35.0, 0.0));
874+
await gesture.up();
875+
876+
expect(valuesList.isNotEmpty, isTrue);
877+
expect(valuesList.last, const RangeValues(29.0, 35.0));
878+
for (final val in valuesList) {
879+
expect(val.start, equals(val.start.roundToDouble()));
880+
expect(val.end, equals(val.end.roundToDouble()));
881+
}
882+
});
883+
840884
testWidgets(
841885
'Range Slider thumbs can be dragged together and the end thumb can be dragged apart (continuous LTR)',
842886
(WidgetTester tester) async {

‎packages/material_ui/test/slider_test.dart‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,47 @@ 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+
expect(values.last, 29.0);
718+
for (final val in values) {
719+
expect(val, equals(val.roundToDouble()));
720+
}
721+
});
722+
682723
testWidgets('Slider can be given zero values', (WidgetTester tester) async {
683724
final log = <double>[];
684725
await tester.pumpWidget(

0 commit comments

Comments
 (0)