From db5c1434e649dc7e203affc4c2b7af512c7bc8c4 Mon Sep 17 00:00:00 2001 From: flyboy Date: Thu, 30 May 2024 22:49:13 +0800 Subject: [PATCH] Fix `Slider` throws an error when `_labelPainter` text is null (#148462) *Fix #148159* --- packages/flutter/lib/src/material/slider.dart | 2 +- .../lib/src/material/slider_theme.dart | 1 - .../flutter/test/material/slider_test.dart | 43 +++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/slider.dart b/packages/flutter/lib/src/material/slider.dart index bb63e2f46a3cf..23cd273b1dbd7 100644 --- a/packages/flutter/lib/src/material/slider.dart +++ b/packages/flutter/lib/src/material/slider.dart @@ -1720,7 +1720,7 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin { if (isInteractive && label != null && !_valueIndicatorAnimation.isDismissed) { if (showValueIndicator) { _state.paintValueIndicator = (PaintingContext context, Offset offset) { - if (attached) { + if (attached && _labelPainter.text != null) { _sliderTheme.valueIndicatorShape!.paint( context, offset + thumbCenter, diff --git a/packages/flutter/lib/src/material/slider_theme.dart b/packages/flutter/lib/src/material/slider_theme.dart index dd979843374b2..aa3af71419396 100644 --- a/packages/flutter/lib/src/material/slider_theme.dart +++ b/packages/flutter/lib/src/material/slider_theme.dart @@ -3485,7 +3485,6 @@ class _DropSliderValueIndicatorPathPainter { return; } assert(!sizeWithOverflow.isEmpty); - final double rectangleWidth = _upperRectangleWidth(labelPainter, scale); final double horizontalShift = getHorizontalShift( parentBox: parentBox, diff --git a/packages/flutter/test/material/slider_test.dart b/packages/flutter/test/material/slider_test.dart index 54656adaa578f..cb26860d91507 100644 --- a/packages/flutter/test/material/slider_test.dart +++ b/packages/flutter/test/material/slider_test.dart @@ -4384,4 +4384,47 @@ void main() { await gesture.moveBy(const Offset(1.0, 0.0)); expect(onChangeCallbackCount, 1); }); + + testWidgets('Skip drawing ValueIndicator shape when label painter text is null', (WidgetTester tester) async { + double sliderValue = 10; + + await tester.pumpWidget( + MaterialApp( + home: StatefulBuilder( + builder: (BuildContext context, void Function(void Function()) setState) { + return Material( + child: Slider( + value: sliderValue, + max: 100, + label: sliderValue > 50 ? null : sliderValue.toString(), + divisions: 10, + onChanged: (double value) { + setState(() { + sliderValue = value; + }); + }, + ), + ); + }, + ), + ), + ); + + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); + + // Calculate a specific position on the Slider. + final Rect sliderRect = tester.getRect(find.byType(Slider)); + final Offset tapPositionLeft = Offset(sliderRect.left + sliderRect.width * 0.25, sliderRect.center.dy); + final Offset tapPositionRight = Offset(sliderRect.left + sliderRect.width * 0.75, sliderRect.center.dy); + + // Tap on the 25% position of the Slider. + await tester.tapAt(tapPositionLeft); + await tester.pumpAndSettle(); + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 2)); + + // Tap on the 75% position of the Slider. + await tester.tapAt(tapPositionRight); + await tester.pumpAndSettle(); + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1)); + }); }