diff --git a/packages/flutter/lib/src/material/slider_theme.dart b/packages/flutter/lib/src/material/slider_theme.dart index 8164a825d8..218627c13c 100644 --- a/packages/flutter/lib/src/material/slider_theme.dart +++ b/packages/flutter/lib/src/material/slider_theme.dart @@ -1453,13 +1453,13 @@ abstract class BaseSliderTrackShape { final double trackHeight = sliderTheme.trackHeight; assert(overlayWidth >= 0); assert(trackHeight >= 0); - assert(parentBox.size.width >= overlayWidth); - assert(parentBox.size.height >= trackHeight); final double trackLeft = offset.dx + overlayWidth / 2; final double trackTop = offset.dy + (parentBox.size.height - trackHeight) / 2; - final double trackWidth = parentBox.size.width - math.max(thumbWidth, overlayWidth); - return Rect.fromLTWH(trackLeft, trackTop, trackWidth, trackHeight); + final double trackRight = trackLeft + parentBox.size.width - math.max(thumbWidth, overlayWidth); + final double trackBottom = trackTop + trackHeight; + // If the parentBox'size less than slider's size the trackRight will be less than trackLeft, so switch them. + return Rect.fromLTRB(math.min(trackLeft, trackRight), trackTop, math.max(trackLeft, trackRight), trackBottom); } } @@ -1735,13 +1735,13 @@ class RectangularRangeSliderTrackShape extends RangeSliderTrackShape { final double trackHeight = sliderTheme.trackHeight; assert(overlayWidth >= 0); assert(trackHeight >= 0); - assert(parentBox.size.width >= overlayWidth); - assert(parentBox.size.height >= trackHeight); final double trackLeft = offset.dx + overlayWidth / 2; final double trackTop = offset.dy + (parentBox.size.height - trackHeight) / 2; - final double trackWidth = parentBox.size.width - overlayWidth; - return Rect.fromLTWH(trackLeft, trackTop, trackWidth, trackHeight); + final double trackRight = trackLeft + parentBox.size.width - overlayWidth; + final double trackBottom = trackTop + trackHeight; + // If the parentBox'size less than slider's size the trackRight will be less than trackLeft, so switch them. + return Rect.fromLTRB(math.min(trackLeft, trackRight), trackTop, math.max(trackLeft, trackRight), trackBottom); } @override @@ -1791,8 +1791,6 @@ class RectangularRangeSliderTrackShape extends RangeSliderTrackShape { rightThumbOffset = startThumbCenter; break; } - final Size thumbSize = sliderTheme.rangeThumbShape.getPreferredSize(isEnabled, isDiscrete); - final double thumbRadius = thumbSize.width / 2; final Rect trackRect = getPreferredRect( parentBox: parentBox, @@ -1801,13 +1799,13 @@ class RectangularRangeSliderTrackShape extends RangeSliderTrackShape { isEnabled: isEnabled, isDiscrete: isDiscrete, ); - final Rect leftTrackSegment = Rect.fromLTRB(trackRect.left, trackRect.top, leftThumbOffset.dx - thumbRadius, trackRect.bottom); + final Rect leftTrackSegment = Rect.fromLTRB(trackRect.left, trackRect.top, leftThumbOffset.dx, trackRect.bottom); if (!leftTrackSegment.isEmpty) context.canvas.drawRect(leftTrackSegment, inactivePaint); - final Rect middleTrackSegment = Rect.fromLTRB(leftThumbOffset.dx + thumbRadius, trackRect.top, rightThumbOffset.dx - thumbRadius, trackRect.bottom); + final Rect middleTrackSegment = Rect.fromLTRB(leftThumbOffset.dx, trackRect.top, rightThumbOffset.dx, trackRect.bottom); if (!middleTrackSegment.isEmpty) context.canvas.drawRect(middleTrackSegment, activePaint); - final Rect rightTrackSegment = Rect.fromLTRB(rightThumbOffset.dx + thumbRadius, trackRect.top, trackRect.right, trackRect.bottom); + final Rect rightTrackSegment = Rect.fromLTRB(rightThumbOffset.dx, trackRect.top, trackRect.right, trackRect.bottom); if (!rightTrackSegment.isEmpty) context.canvas.drawRect(rightTrackSegment, inactivePaint); } @@ -1865,13 +1863,13 @@ class RoundedRectRangeSliderTrackShape extends RangeSliderTrackShape { final double trackHeight = sliderTheme.trackHeight; assert(overlayWidth >= 0); assert(trackHeight >= 0); - assert(parentBox.size.width >= overlayWidth); - assert(parentBox.size.height >= trackHeight); final double trackLeft = offset.dx + overlayWidth / 2; final double trackTop = offset.dy + (parentBox.size.height - trackHeight) / 2; - final double trackWidth = parentBox.size.width - overlayWidth; - return Rect.fromLTWH(trackLeft, trackTop, trackWidth, trackHeight); + final double trackRight = trackLeft + parentBox.size.width - overlayWidth; + final double trackBottom = trackTop + trackHeight; + // If the parentBox'size less than slider's size the trackRight will be less than trackLeft, so switch them. + return Rect.fromLTRB(math.min(trackLeft, trackRight), trackTop, math.max(trackLeft, trackRight), trackBottom); } @override @@ -2311,7 +2309,6 @@ class RoundSliderThumbShape extends SliderComponentShape { assert(sliderTheme != null); assert(sliderTheme.disabledThumbColor != null); assert(sliderTheme.thumbColor != null); - assert(!sizeWithOverflow.isEmpty); final Canvas canvas = context.canvas; final Tween radiusTween = Tween( diff --git a/packages/flutter/test/material/range_slider_test.dart b/packages/flutter/test/material/range_slider_test.dart index 6ec65fd9ba..3d4f72a9d8 100644 --- a/packages/flutter/test/material/range_slider_test.dart +++ b/packages/flutter/test/material/range_slider_test.dart @@ -1833,4 +1833,86 @@ void main() { 'inactiveColor: MaterialColor(primary value: Color(0xff9e9e9e))', ]); }); + + testWidgets('Range Slider can be painted in a narrower constraint when track shape is RoundedRectRange', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Directionality( + textDirection: TextDirection.ltr, + child: Material( + child: Center( + child: SizedBox( + height: 10.0, + width: 0.0, + child: RangeSlider( + values: const RangeValues(0.25, 0.5), + onChanged: null, + ), + ), + ), + ), + ), + ), + ); + + // _RenderRangeSlider is the last render object in the tree. + final RenderObject renderObject = tester.allRenderObjects.last; + + expect(renderObject, + paints + // left inactive track RRect + ..rrect(rrect: RRect.fromLTRBAndCorners(-24.0, 3.0, -12.0, 7.0, topLeft: const Radius.circular(2.0), bottomLeft: const Radius.circular(2.0))) + // active track RRect + ..rect(rect: const Rect.fromLTRB(-12.0, 2.0, 0.0, 8.0)) + // right inactive track RRect + ..rrect(rrect: RRect.fromLTRBAndCorners(0.0, 3.0, 24.0, 7.0, topRight: const Radius.circular(2.0), bottomRight: const Radius.circular(2.0))) + // thumbs + ..circle(x: -12.0, y: 5.0, radius: 10.0,) + ..circle(x: 0.0, y: 5.0, radius: 10.0,) + ); + }); + + testWidgets('Range Slider can be painted in a narrower constraint when track shape is Rectangular', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + theme: ThemeData( + sliderTheme: const SliderThemeData( + rangeTrackShape: RectangularRangeSliderTrackShape(), + ) + ), + home: Directionality( + textDirection: TextDirection.ltr, + child: Material( + child: Center( + child: SizedBox( + height: 10.0, + width: 0.0, + child: RangeSlider( + values: const RangeValues(0.25, 0.5), + onChanged: null, + ), + ), + ), + ), + ), + ), + ); + + // _RenderRangeSlider is the last render object in the tree. + final RenderObject renderObject = tester.allRenderObjects.last; + + //There should no gap between the inactive track and active track. + expect(renderObject, + paints + // left inactive track RRect + ..rect(rect: const Rect.fromLTRB(-24.0, 3.0, -12.0, 7.0)) + // active track RRect + ..rect(rect: const Rect.fromLTRB(-12.0, 3.0, 0.0, 7.0)) + // right inactive track RRect + ..rect(rect: const Rect.fromLTRB(0.0, 3.0, 24.0, 7.0)) + // thumbs + ..circle(x: -12.0, y: 5.0, radius: 10.0,) + ..circle(x: 0.0, y: 5.0, radius: 10.0,) + ); + }); } diff --git a/packages/flutter/test/material/slider_test.dart b/packages/flutter/test/material/slider_test.dart index 053927935e..886d4c6b96 100644 --- a/packages/flutter/test/material/slider_test.dart +++ b/packages/flutter/test/material/slider_test.dart @@ -2360,13 +2360,13 @@ void main() { ]); }); - testWidgets('Slider paint correctly when the track shape is rectangular', (WidgetTester tester) async { + testWidgets('Slider track paints correctly when the shape is rectangular', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp( theme: ThemeData( sliderTheme: const SliderThemeData( trackShape: RectangularSliderTrackShape(), - ) + ), ), home: Directionality( textDirection: TextDirection.ltr, @@ -2396,4 +2396,39 @@ void main() { ..rect(rect: const Rect.fromLTRB(400.0, 298.0, 776.0, 302.0)) // inactive track Rect. ); }); + + testWidgets('Slider can be painted in a narrower constraint', (WidgetTester tester) async { + await tester.pumpWidget( + const MaterialApp( + home: Directionality( + textDirection: TextDirection.ltr, + child: Material( + child: Center( + child: SizedBox( + height: 10.0, + width: 10.0, + child: Slider( + value: 0.5, + onChanged: null, + ), + ), + ), + ), + ), + ), + ); + + // _RenderSlider is the last render object in the tree. + final RenderObject renderObject = tester.allRenderObjects.last; + + expect(renderObject, + paints + // active track RRect + ..rrect(rrect: RRect.fromLTRBAndCorners(-14.0, 2.0, 5.0, 8.0, topLeft: const Radius.circular(3.0), bottomLeft: const Radius.circular(3.0))) + // inactive track RRect + ..rrect(rrect: RRect.fromLTRBAndCorners(5.0, 3.0, 24.0, 7.0, topRight: const Radius.circular(2.0), bottomRight: const Radius.circular(2.0))) + // thumb + ..circle(x: 5.0, y: 5.0, radius: 10.0, ) + ); + }); }