Skip to content

Commit 5b7526b

Browse files
hello-coder-xuTahaTesser
authored andcommitted
fix slider text null error
1 parent d02292d commit 5b7526b

File tree

2 files changed

+55
-1
lines changed

2 files changed

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

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

0 commit comments

Comments
 (0)