From d40f4399e260b3d3516b0c2c9230cd9c045becea Mon Sep 17 00:00:00 2001 From: xubaolin Date: Fri, 29 Jan 2021 01:04:06 +0800 Subject: [PATCH] Fix a slider layout bug when the overlay size is smaller than the thumb size (#74880) --- .../lib/src/material/slider_theme.dart | 2 +- .../test/material/slider_theme_test.dart | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/slider_theme.dart b/packages/flutter/lib/src/material/slider_theme.dart index 65367d532e5..c307e4063b6 100644 --- a/packages/flutter/lib/src/material/slider_theme.dart +++ b/packages/flutter/lib/src/material/slider_theme.dart @@ -1451,7 +1451,7 @@ abstract class BaseSliderTrackShape { assert(overlayWidth >= 0); assert(trackHeight >= 0); - final double trackLeft = offset.dx + overlayWidth / 2; + final double trackLeft = offset.dx + math.max(overlayWidth / 2, thumbWidth / 2); final double trackTop = offset.dy + (parentBox.size.height - trackHeight) / 2; final double trackRight = trackLeft + parentBox.size.width - math.max(thumbWidth, overlayWidth); final double trackBottom = trackTop + trackHeight; diff --git a/packages/flutter/test/material/slider_theme_test.dart b/packages/flutter/test/material/slider_theme_test.dart index 1e4a4bbe768..6ad93ddced4 100644 --- a/packages/flutter/test/material/slider_theme_test.dart +++ b/packages/flutter/test/material/slider_theme_test.dart @@ -852,6 +852,47 @@ void main() { ); }); + // Regression test for https://github.com/flutter/flutter/issues/74503 + testWidgets('The slider track layout correctly when the overlay size is smaller than the thumb size', (WidgetTester tester) async { + final SliderThemeData sliderTheme = ThemeData().sliderTheme.copyWith( + overlayShape: SliderComponentShape.noOverlay, + ); + + await tester.pumpWidget(_buildApp(sliderTheme, value: 0.5)); + + final MaterialInkController material = Material.of( + tester.element(find.byType(Slider)), + )!; + + // The track rectangle begins at 10 pixels from the left of the screen and ends 10 pixels from the right + // (790 pixels from the left). The main check here it that the track itself should be centered on + // the 800 pixel-wide screen. + expect( + material, + paints + // active track RRect. Starts 10 pixels from left of screen. + ..rrect(rrect: RRect.fromLTRBAndCorners( + 10.0, + 297.0, + 400.0, + 303.0, + topLeft: const Radius.circular(3.0), + bottomLeft: const Radius.circular(3.0), + )) + // inactive track RRect. Ends 10 pixels from right of screen. + ..rrect(rrect: RRect.fromLTRBAndCorners( + 400.0, + 298.0, + 790.0, + 302.0, + topRight: const Radius.circular(2.0), + bottomRight: const Radius.circular(2.0), + )) + // The thumb. + ..circle(x: 400.0, y: 300.0, radius: 10.0, ) + ); + }); + // Only the thumb, overlay, and tick mark have special shortcuts to provide // no-op or empty shapes. //