Skip to content

Commit

Permalink
Fix Slider throws an error when _labelPainter text is null (flutt…
Browse files Browse the repository at this point in the history
  • Loading branch information
hello-coder-xu authored May 30, 2024
1 parent d4e9b78 commit db5c143
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/material/slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion packages/flutter/lib/src/material/slider_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3485,7 +3485,6 @@ class _DropSliderValueIndicatorPathPainter {
return;
}
assert(!sizeWithOverflow.isEmpty);

final double rectangleWidth = _upperRectangleWidth(labelPainter, scale);
final double horizontalShift = getHorizontalShift(
parentBox: parentBox,
Expand Down
43 changes: 43 additions & 0 deletions packages/flutter/test/material/slider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}

0 comments on commit db5c143

Please sign in to comment.