From 73194cd9f2684f1434592fe08d6d70d5c1eccdfc Mon Sep 17 00:00:00 2001 From: xubaolin Date: Sat, 19 Sep 2020 05:52:03 +0800 Subject: [PATCH] Fix bug when updating the `divisions` and `value` of the slider at the same time (#65998) --- .../lib/src/material/range_slider.dart | 4 +- packages/flutter/lib/src/material/slider.dart | 4 +- .../test/material/range_slider_test.dart | 44 +++++++++++++++++++ .../flutter/test/material/slider_test.dart | 41 +++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/range_slider.dart b/packages/flutter/lib/src/material/range_slider.dart index 4b76db7282..53e1b7d596 100644 --- a/packages/flutter/lib/src/material/range_slider.dart +++ b/packages/flutter/lib/src/material/range_slider.dart @@ -731,8 +731,10 @@ class _RangeSliderRenderObjectWidget extends LeafRenderObjectWidget { @override void updateRenderObject(BuildContext context, _RenderRangeSlider renderObject) { renderObject - ..values = values + // We should update the `divisions` ahead of `values`, because the `values` + // setter dependent on the `divisions`. ..divisions = divisions + ..values = values ..labels = labels ..sliderTheme = sliderTheme ..theme = Theme.of(context) diff --git a/packages/flutter/lib/src/material/slider.dart b/packages/flutter/lib/src/material/slider.dart index 201c47d128..45db4d16f1 100644 --- a/packages/flutter/lib/src/material/slider.dart +++ b/packages/flutter/lib/src/material/slider.dart @@ -839,8 +839,10 @@ class _SliderRenderObjectWidget extends LeafRenderObjectWidget { @override void updateRenderObject(BuildContext context, _RenderSlider renderObject) { renderObject - ..value = value + // We should update the `divisions` ahead of `value`, because the `value` + // setter dependent on the `divisions`. ..divisions = divisions + ..value = value ..label = label ..sliderTheme = sliderTheme ..theme = Theme.of(context) diff --git a/packages/flutter/test/material/range_slider_test.dart b/packages/flutter/test/material/range_slider_test.dart index 3d4f72a9d8..ca2b8013b0 100644 --- a/packages/flutter/test/material/range_slider_test.dart +++ b/packages/flutter/test/material/range_slider_test.dart @@ -10,6 +10,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; +import 'package:flutter/src/physics/utils.dart' show nearEqual; import 'package:flutter_test/flutter_test.dart'; import '../rendering/mock_canvas.dart'; @@ -1915,4 +1916,47 @@ void main() { ..circle(x: 0.0, y: 5.0, radius: 10.0,) ); }); + + testWidgets('Update the divisions and values at the same time for RangeSlider', (WidgetTester tester) async { + // Regress test for https://github.com/flutter/flutter/issues/65943 + Widget buildFrame(double maxValue) { + return MaterialApp( + home: Material( + child: Center( + child: RangeSlider( + values: const RangeValues(5, 8), + max: maxValue, + divisions: maxValue.toInt(), + onChanged: (RangeValues newValue) {}, + ), + ), + ), + ); + } + + await tester.pumpWidget(buildFrame(10)); + + // _RenderRangeSlider is the last render object in the tree. + final RenderObject renderObject = tester.allRenderObjects.last; + + // Update the divisions from 10 to 15, the thumbs should be paint at the correct position. + await tester.pumpWidget(buildFrame(15)); + await tester.pumpAndSettle(); // Finish the animation. + + Rect activeTrackRect; + expect(renderObject, paints..something((Symbol method, List arguments) { + if (method != #drawRect) + return false; + activeTrackRect = arguments[0] as Rect; + return true; + })); + + // The 1st thumb should at one-third(5 / 15) of the Slider. + // The 2nd thumb should at (8 / 15) of the Slider. + // The left of the active track shape is the position of the 1st thumb. + // The right of the active track shape is the position of the 2nd thumb. + // 24.0 is the default margin, (800.0 - 24.0 - 24.0) is the slider's width. + expect(nearEqual(activeTrackRect.left, (800.0 - 24.0 - 24.0) * (5 / 15) + 24.0, 0.01), true); + expect(nearEqual(activeTrackRect.right, (800.0 - 24.0 - 24.0) * (8 / 15) + 24.0, 0.01), true); + }); } diff --git a/packages/flutter/test/material/slider_test.dart b/packages/flutter/test/material/slider_test.dart index 886d4c6b96..64dd9787b1 100644 --- a/packages/flutter/test/material/slider_test.dart +++ b/packages/flutter/test/material/slider_test.dart @@ -12,6 +12,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; +import 'package:flutter/src/physics/utils.dart' show nearEqual; import 'package:flutter_test/flutter_test.dart'; import '../rendering/mock_canvas.dart'; @@ -2431,4 +2432,44 @@ void main() { ..circle(x: 5.0, y: 5.0, radius: 10.0, ) ); }); + + testWidgets('Update the divisions and value at the same time for Slider', (WidgetTester tester) async { + // Regress test for https://github.com/flutter/flutter/issues/65943 + Widget buildFrame(double maxValue) { + return MaterialApp( + home: Material( + child: Center( + child: Slider.adaptive( + value: 5, + max: maxValue, + divisions: maxValue.toInt(), + onChanged: (double newValue) {}, + ), + ), + ), + ); + } + + await tester.pumpWidget(buildFrame(10)); + + // _RenderSlider is the last render object in the tree. + final RenderObject renderObject = tester.allRenderObjects.last; + + // Update the divisions from 10 to 15, the thumb should be paint at the correct position. + await tester.pumpWidget(buildFrame(15)); + await tester.pumpAndSettle(); // Finish the animation. + + RRect activeTrackRRect; + expect(renderObject, paints..something((Symbol method, List arguments) { + if (method != #drawRRect) + return false; + activeTrackRRect = arguments[0] as RRect; + return true; + })); + + // The thumb should at one-third(5 / 15) of the Slider. + // The right of the active track shape is the position of the thumb. + // 24.0 is the default margin, (800.0 - 24.0 - 24.0) is the slider's width. + expect(nearEqual(activeTrackRRect.right, (800.0 - 24.0 - 24.0) * (5 / 15) + 24.0, 0.01), true); + }); }