Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Material] Unit test for skipping Slider tick mark due to overdensity #28013

Merged
merged 16 commits into from Feb 26, 2019
Merged
14 changes: 7 additions & 7 deletions packages/flutter/lib/src/material/slider.dart
Expand Up @@ -1005,16 +1005,16 @@ class _RenderSlider extends RenderBox {
isEnabled: isInteractive,
sliderTheme: _sliderTheme,
).width;
// If the ticks would be too dense, don't bother painting them.
if ((trackRect.width - tickMarkWidth) / divisions >= 3.0 * tickMarkWidth) {
final double adjustedTrackWidth = trackRect.width - tickMarkWidth;
// If the tick marks would be too dense, don't bother painting them.
if (adjustedTrackWidth / divisions >= 3.0 * tickMarkWidth) {
final double dy = trackRect.center.dy;
for (int i = 0; i <= divisions; i++) {
final double tickValue = i / divisions;
final double value = i / divisions;
// The ticks are mapped to be within the track, so the tick mark width
// must be subtracted from the track width.
final double tickX = trackRect.left +
tickValue * (trackRect.width - tickMarkWidth) + tickMarkWidth / 2;
final double tickY = trackRect.center.dy;
final Offset tickMarkOffset = Offset(tickX, tickY);
final double dx = trackRect.left + value * adjustedTrackWidth + tickMarkWidth / 2;
final Offset tickMarkOffset = Offset(dx, dy);
_sliderTheme.tickMarkShape.paint(
context,
tickMarkOffset,
Expand Down
53 changes: 51 additions & 2 deletions packages/flutter/test/material/slider_test.dart
Expand Up @@ -558,8 +558,8 @@ void main() {
final ValueChanged<double> onChanged = !enabled
? null
: (double d) {
value = d;
};
value = d;
};
return Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
Expand Down Expand Up @@ -978,6 +978,55 @@ void main() {
await tester.pumpAndSettle();
});

testWidgets('Tick marks are skipped when they are too dense', (WidgetTester tester) async {
Widget buildSlider({
int divisions,
}) {
return Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: MediaQueryData.fromWindow(window),
child: Material(
child: Center(
child: Slider(
min: 0.0,
max: 100.0,
divisions: divisions,
value: 0.25,
onChanged: (double newValue) { },
),
),
),
),
);
}

// Pump a slider with a reasonable amount of divisions to verify that the
// tick marks are drawn when the number of tick marks is not too dense.
await tester.pumpWidget(
buildSlider(
divisions: 4,
),
);

final RenderBox sliderBox = tester.firstRenderObject<RenderBox>(find.byType(Slider));

// 5 tick marks and a thumb.
expect(sliderBox, paintsExactlyCountTimes(#drawCircle, 6));

// 200 divisions will produce a tick interval off less than 6,
// which would be too dense to draw.
await tester.pumpWidget(
buildSlider(
divisions: 200,
),
);

// No tick marks are drawn because they are too dense, but the thumb is
// still drawn.
expect(sliderBox, paintsExactlyCountTimes(#drawCircle, 1));
});

testWidgets('Slider has correct animations when reparented', (WidgetTester tester) async {
final Key sliderKey = GlobalKey(debugLabel: 'A');
double value = 0.0;
Expand Down