Skip to content

Commit

Permalink
[Material] Unit test for skipping Slider tick mark due to overdensity (
Browse files Browse the repository at this point in the history
…#28013)

Added a unit test to check that the tick marks are skipped over when the divisions are set to a number that makes them less than 6dp away from each other.
  • Loading branch information
clocksmith committed Feb 26, 2019
1 parent b09e64e commit 70c8b63
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
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

0 comments on commit 70c8b63

Please sign in to comment.