Skip to content

Commit 249f37d

Browse files
authored
[material_ui] Paint the active RoundedRectSliderTrackShape segment on top in RTL (flutter#13225)
`RoundedRectSliderTrackShape` paints its two segments in a fixed order: the right-hand segment first, then the left-hand one. Both segments extend past the thumb center by half of the track height, so the segment painted last has its rounded end drawn over the other one. In LTR the left-hand segment is the active one, so the active segment's rounded end is on top. In RTL the left-hand segment is the inactive one, so the inactive segment's rounded end was painted over the active segment, which left a pale sliver beside the thumb. This is only visible when the track is thicker than the thumb. This PR paints the inactive segment first in both text directions. The LTR paint order is unchanged. The rounded `RangeSlider` track already paints its active segment last in both directions. Before and after, LTR on top and RTL below, with `trackHeight: 24`: <details open> <summary>Screenshots demonstration</summary> | State | Direction | Screenshot | | :---: | :---: | :---: | | **Before** | <kbd>LTR ➔</kbd><br><br><br><kbd>⬅ RTL</kbd> | <img width="1200" height="320" alt="1_rounded_BEFORE" src="https://github.com/user-attachments/assets/8a4f0460-657c-4d53-8e1f-1534a850bcb1" /> | | **After** | <kbd>LTR ➔</kbd><br><br><br><kbd>⬅ RTL</kbd> | <img width="1200" height="320" alt="1_rounded_AFTER" src="https://github.com/user-attachments/assets/25ec473c-a9f9-4c27-9380-7a2c728183c4" /> | </details> I added an RTL case to the existing `RoundedRectSliderTrackShape` test group. It checks the exact track rects and their paint order, and it fails without the fix. Fixes flutter/flutter#194278 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent fc8b069 commit 249f37d

3 files changed

Lines changed: 73 additions & 24 deletions

File tree

‎packages/material_ui/lib/src/slider_parts.dart‎

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ class RectangularSliderTrackShape extends SliderTrackShape with BaseSliderTrackS
396396
}
397397
}
398398

399+
// A horizontal span of the track, and whether it is wide enough to be painted.
400+
typedef _TrackSegment = ({bool draw, double left, double right});
401+
399402
/// The default shape of a [Slider]'s track.
400403
///
401404
/// It paints a solid colored rectangle with rounded edges, vertically centered
@@ -463,10 +466,6 @@ class RoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackS
463466
);
464467
final activePaint = Paint()..color = activeTrackColorTween.evaluate(enableAnimation)!;
465468
final inactivePaint = Paint()..color = inactiveTrackColorTween.evaluate(enableAnimation)!;
466-
final (Paint leftTrackPaint, Paint rightTrackPaint) = switch (textDirection) {
467-
TextDirection.ltr => (activePaint, inactivePaint),
468-
TextDirection.rtl => (inactivePaint, activePaint),
469-
};
470469

471470
final Rect trackRect = getPreferredRect(
472471
parentBox: parentBox,
@@ -478,35 +477,48 @@ class RoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackS
478477
final trackRadius = Radius.circular(trackRect.height / 2);
479478
final activeTrackRadius = Radius.circular((trackRect.height + additionalActiveTrackHeight) / 2);
480479
final isLTR = textDirection == TextDirection.ltr;
481-
final isRTL = textDirection == TextDirection.rtl;
482480

483-
final bool drawInactiveTrack =
484-
thumbCenter.dx < (trackRect.right - (sliderTheme.trackHeight! / 2));
485-
if (drawInactiveTrack) {
486-
// Draw the inactive track segment.
481+
// Both segments extend past the thumb center by half of the track height.
482+
final double halfTrackHeight = sliderTheme.trackHeight! / 2;
483+
final _TrackSegment leftTrack = (
484+
draw: thumbCenter.dx > trackRect.left + halfTrackHeight,
485+
left: trackRect.left,
486+
right: thumbCenter.dx + halfTrackHeight,
487+
);
488+
final _TrackSegment rightTrack = (
489+
draw: thumbCenter.dx < trackRect.right - halfTrackHeight,
490+
left: thumbCenter.dx - halfTrackHeight,
491+
right: trackRect.right,
492+
);
493+
final (_TrackSegment inactiveTrack, _TrackSegment activeTrack) = isLTR
494+
? (rightTrack, leftTrack)
495+
: (leftTrack, rightTrack);
496+
497+
// The segment painted last has its rounded end drawn on top of the other one,
498+
// so paint the inactive segment first to keep the active one on top in both
499+
// text directions.
500+
if (inactiveTrack.draw) {
487501
context.canvas.drawRRect(
488502
RRect.fromLTRBR(
489-
thumbCenter.dx - (sliderTheme.trackHeight! / 2),
490-
isRTL ? trackRect.top - (additionalActiveTrackHeight / 2) : trackRect.top,
491-
trackRect.right,
492-
isRTL ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
493-
isLTR ? trackRadius : activeTrackRadius,
503+
inactiveTrack.left,
504+
trackRect.top,
505+
inactiveTrack.right,
506+
trackRect.bottom,
507+
trackRadius,
494508
),
495-
rightTrackPaint,
509+
inactivePaint,
496510
);
497511
}
498-
final bool drawActiveTrack = thumbCenter.dx > (trackRect.left + (sliderTheme.trackHeight! / 2));
499-
if (drawActiveTrack) {
500-
// Draw the active track segment.
512+
if (activeTrack.draw) {
501513
context.canvas.drawRRect(
502514
RRect.fromLTRBR(
503-
trackRect.left,
504-
isLTR ? trackRect.top - (additionalActiveTrackHeight / 2) : trackRect.top,
505-
thumbCenter.dx + (sliderTheme.trackHeight! / 2),
506-
isLTR ? trackRect.bottom + (additionalActiveTrackHeight / 2) : trackRect.bottom,
507-
isLTR ? activeTrackRadius : trackRadius,
515+
activeTrack.left,
516+
trackRect.top - (additionalActiveTrackHeight / 2),
517+
activeTrack.right,
518+
trackRect.bottom + (additionalActiveTrackHeight / 2),
519+
activeTrackRadius,
508520
),
509-
leftTrackPaint,
521+
activePaint,
510522
);
511523
}
512524

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `RoundedRectSliderTrackShape` painting the inactive track segment over the end of the active one in right-to-left layouts.
3+
version: patch

‎packages/material_ui/test/slider_theme_test.dart‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,40 @@ void main() {
26772677
);
26782678
},
26792679
);
2680+
2681+
testWidgets('Active track is painted on top of the inactive track in RTL', (
2682+
WidgetTester tester,
2683+
) async {
2684+
// Regression test for https://github.com/flutter/flutter/issues/194278
2685+
await tester.pumpWidget(
2686+
MaterialApp(
2687+
home: Directionality(
2688+
textDirection: TextDirection.rtl,
2689+
child: Material(
2690+
child: Center(
2691+
child: SliderTheme(
2692+
data: const SliderThemeData(trackShape: RoundedRectSliderTrackShape()),
2693+
child: Slider(value: 0.5, onChanged: (double value) {}),
2694+
),
2695+
),
2696+
),
2697+
),
2698+
),
2699+
);
2700+
2701+
// Both track segments extend past the thumb center by half of the track
2702+
// height, so the inactive track must be painted first for the rounded end
2703+
// of the active track not to be covered by it.
2704+
final MaterialInkController material = Material.of(tester.element(find.byType(Slider)));
2705+
expect(
2706+
material,
2707+
paints
2708+
// Inactive track.
2709+
..rrect(rrect: RRect.fromLTRBR(24.0, 298.0, 402.0, 302.0, const Radius.circular(2.0)))
2710+
// Active track.
2711+
..rrect(rrect: RRect.fromLTRBR(398.0, 297.0, 776.0, 303.0, const Radius.circular(3.0))),
2712+
);
2713+
});
26802714
});
26812715

26822716
testWidgets('Track shape isRounded defaults', (WidgetTester tester) async {

0 commit comments

Comments
 (0)