Skip to content

Commit 6c74917

Browse files
author
yinxu
committed
fix slider text null error
1 parent 99dcbf8 commit 6c74917

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3485,7 +3485,10 @@ class _DropSliderValueIndicatorPathPainter {
34853485
return;
34863486
}
34873487
assert(!sizeWithOverflow.isEmpty);
3488-
3488+
if (labelPainter.text == null) {
3489+
// labelPainter.text is null do not draw anything, so it's safe to just return.
3490+
return;
3491+
}
34893492
final double rectangleWidth = _upperRectangleWidth(labelPainter, scale);
34903493
final double horizontalShift = getHorizontalShift(
34913494
parentBox: parentBox,

packages/flutter/test/material/slider_theme_test.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,6 +2648,36 @@ void main() {
26482648
}
26492649
});
26502650
});
2651+
2652+
testWidgets('indicator shape skips all painting at label is null',
2653+
(WidgetTester tester) async {
2654+
debugDisableShadows = false;
2655+
try {
2656+
await tester.pumpWidget(_buildSilderApp());
2657+
2658+
final RenderBox valueIndicatorBox =
2659+
tester.renderObject(find.byType(Overlay));
2660+
2661+
// Calculate a specific position on the Slider
2662+
final Rect sliderRect = tester.getRect(find.byType(Slider));
2663+
final Offset tapPositionLeft = Offset(
2664+
sliderRect.left + sliderRect.width * 0.25, sliderRect.center.dy);
2665+
final Offset tapPositionRight = Offset(
2666+
sliderRect.left + sliderRect.width * 0.75, sliderRect.center.dy);
2667+
2668+
// Tap on the 25% position of the Slider
2669+
await tester.tapAt(tapPositionLeft);
2670+
await tester.pumpAndSettle();
2671+
expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1));
2672+
2673+
// Tap on the 75% position of the Slider
2674+
await tester.tapAt(tapPositionRight);
2675+
await tester.pumpAndSettle();
2676+
expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 0));
2677+
} finally {
2678+
debugDisableShadows = true;
2679+
}
2680+
});
26512681
}
26522682

26532683
class RoundedRectSliderTrackShapeWithCustomAdditionalActiveTrackHeight extends RoundedRectSliderTrackShape {
@@ -2722,3 +2752,26 @@ Widget _buildRangeApp(
27222752
),
27232753
);
27242754
}
2755+
2756+
Widget _buildSilderApp() {
2757+
double sliderValue = 10;
2758+
return MaterialApp(
2759+
home: StatefulBuilder(
2760+
builder: (BuildContext context, void Function(void Function()) setState) {
2761+
return Material(
2762+
child: Slider(
2763+
value: sliderValue,
2764+
max: 100,
2765+
label: sliderValue > 50 ? null : sliderValue.toString(),
2766+
divisions: 10,
2767+
onChanged: (double value) {
2768+
setState(() {
2769+
sliderValue = value;
2770+
});
2771+
},
2772+
),
2773+
);
2774+
},
2775+
),
2776+
);
2777+
}

0 commit comments

Comments
 (0)