Fix Slider throws an error when _labelPainter text is null (#148462)

*Fix #148159*
This commit is contained in:
flyboy 2024-05-30 22:49:13 +08:00 committed by GitHub
parent d4e9b78161
commit db5c1434e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 2 deletions

View File

@ -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,

View File

@ -3485,7 +3485,6 @@ class _DropSliderValueIndicatorPathPainter {
return;
}
assert(!sizeWithOverflow.isEmpty);
final double rectangleWidth = _upperRectangleWidth(labelPainter, scale);
final double horizontalShift = getHorizontalShift(
parentBox: parentBox,

View File

@ -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));
});
}