Skip to content

Commit db5c143

Browse files
Fix Slider throws an error when _labelPainter text is null (flutter#148462)
*Fix flutter#148159*
1 parent d4e9b78 commit db5c143

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

packages/flutter/lib/src/material/slider.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
17201720
if (isInteractive && label != null && !_valueIndicatorAnimation.isDismissed) {
17211721
if (showValueIndicator) {
17221722
_state.paintValueIndicator = (PaintingContext context, Offset offset) {
1723-
if (attached) {
1723+
if (attached && _labelPainter.text != null) {
17241724
_sliderTheme.valueIndicatorShape!.paint(
17251725
context,
17261726
offset + thumbCenter,

packages/flutter/lib/src/material/slider_theme.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3485,7 +3485,6 @@ class _DropSliderValueIndicatorPathPainter {
34853485
return;
34863486
}
34873487
assert(!sizeWithOverflow.isEmpty);
3488-
34893488
final double rectangleWidth = _upperRectangleWidth(labelPainter, scale);
34903489
final double horizontalShift = getHorizontalShift(
34913490
parentBox: parentBox,

packages/flutter/test/material/slider_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4384,4 +4384,47 @@ void main() {
43844384
await gesture.moveBy(const Offset(1.0, 0.0));
43854385
expect(onChangeCallbackCount, 1);
43864386
});
4387+
4388+
testWidgets('Skip drawing ValueIndicator shape when label painter text is null', (WidgetTester tester) async {
4389+
double sliderValue = 10;
4390+
4391+
await tester.pumpWidget(
4392+
MaterialApp(
4393+
home: StatefulBuilder(
4394+
builder: (BuildContext context, void Function(void Function()) setState) {
4395+
return Material(
4396+
child: Slider(
4397+
value: sliderValue,
4398+
max: 100,
4399+
label: sliderValue > 50 ? null : sliderValue.toString(),
4400+
divisions: 10,
4401+
onChanged: (double value) {
4402+
setState(() {
4403+
sliderValue = value;
4404+
});
4405+
},
4406+
),
4407+
);
4408+
},
4409+
),
4410+
),
4411+
);
4412+
4413+
final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay));
4414+
4415+
// Calculate a specific position on the Slider.
4416+
final Rect sliderRect = tester.getRect(find.byType(Slider));
4417+
final Offset tapPositionLeft = Offset(sliderRect.left + sliderRect.width * 0.25, sliderRect.center.dy);
4418+
final Offset tapPositionRight = Offset(sliderRect.left + sliderRect.width * 0.75, sliderRect.center.dy);
4419+
4420+
// Tap on the 25% position of the Slider.
4421+
await tester.tapAt(tapPositionLeft);
4422+
await tester.pumpAndSettle();
4423+
expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 2));
4424+
4425+
// Tap on the 75% position of the Slider.
4426+
await tester.tapAt(tapPositionRight);
4427+
await tester.pumpAndSettle();
4428+
expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1));
4429+
});
43874430
}

0 commit comments

Comments
 (0)