Skip to content

Commit fd8d150

Browse files
authored
[material_ui] Port flutter/flutter #185149 "Slider label clips the screen" (#12572)
Ports flutter/flutter#185149 from flutter/flutter to flutter/packages, following the porting instructions in flutter/flutter#188444. Long text in a `Slider` or `RangeSlider` label clipped off the edges of the screen. This limits the label's `maxWidth` to the screen width minus a calculated horizontal buffer (`_kValueIndicatorHorizontalBuffer`), forces `maxLines = 1`, and applies the Unicode ellipsis (`\u2026`), so the text no longer expands past the screen boundary and the bubble shape no longer breaks vertically. A `screenSize.width.isFinite && screenSize.width > 0` guard keeps the constraint inert when no valid size is available. The original PR was reviewed and approved by @QuncCccccc. No merge commits were included; the five commits cherry-picked onto `material_ui` with no conflicts. Validation: - `dart format --set-exit-if-changed` on the four changed files - `git diff --check` - `flutter test test/slider_test.dart test/range_slider_test.dart` Fixes flutter/flutter#63293 ## 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 629f27e commit fd8d150

5 files changed

Lines changed: 130 additions & 6 deletions

File tree

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,13 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
971971
// This value is the touch target, 48, multiplied by 3.
972972
static const double _minPreferredTrackWidth = 144.0;
973973

974+
// Buffer to account for the internal padding of standard Material value indicator shapes,
975+
// preventing them from bleeding off the screen edges.
976+
//
977+
// The value 64.0 is a heuristic that covers the minimum size of the shape
978+
// (padding + minimum label width) at a text scale factor of roughly 2.0.
979+
static const double _kValueIndicatorHorizontalBuffer = 64.0;
980+
974981
// Compute the largest width and height needed to paint the slider shapes,
975982
// other than the track shape. It is assumed that these shapes are vertically
976983
// centered on the track.
@@ -1262,11 +1269,22 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
12621269
Thumb.end => (labels.end, _endLabelPainter),
12631270
};
12641271

1272+
// Reserve space for the bubble's internal padding and screen margins.
1273+
final double safeMaxWidth = math.max(0.0, screenSize.width - _kValueIndicatorHorizontalBuffer);
1274+
12651275
labelPainter
12661276
..text = TextSpan(style: _sliderTheme.valueIndicatorTextStyle, text: text)
12671277
..textDirection = textDirection
1268-
..textScaleFactor = textScaleFactor
1269-
..layout();
1278+
..textScaler = TextScaler.linear(textScaleFactor)
1279+
..maxLines = 1
1280+
..ellipsis =
1281+
'\u2026' // Standard Unicode ellipsis
1282+
..layout(
1283+
maxWidth: screenSize.width.isFinite && screenSize.width > 0
1284+
? safeMaxWidth
1285+
: double.infinity,
1286+
);
1287+
12701288
// Changing the textDirection can result in the layout changing, because the
12711289
// bidi algorithm might line up the glyphs differently which can result in
12721290
// different ligatures, different shapes, etc. So we always markNeedsLayout.

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,13 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
12421242
// This value is the touch target, 48, multiplied by 3.
12431243
static const double _minPreferredTrackWidth = 144.0;
12441244

1245+
// Buffer to account for the internal padding of standard Material value indicator shapes,
1246+
// preventing them from bleeding off the screen edges.
1247+
//
1248+
// The value 64.0 is a heuristic that covers the minimum size of the shape
1249+
// (padding + minimum label width) at a text scale factor of roughly 2.0.
1250+
static const double _kValueIndicatorHorizontalBuffer = 64.0;
1251+
12451252
// Compute the largest width and height needed to paint the slider shapes,
12461253
// other than the track shape. It is assumed that these shapes are vertically
12471254
// centered on the track.
@@ -1533,11 +1540,24 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
15331540

15341541
void _updateLabelPainter() {
15351542
if (label != null) {
1543+
// Reserve space for the bubble's internal padding and screen margins.
1544+
final double safeMaxWidth = math.max(
1545+
0.0,
1546+
screenSize.width - _kValueIndicatorHorizontalBuffer,
1547+
);
1548+
15361549
_labelPainter
15371550
..text = TextSpan(style: _sliderTheme.valueIndicatorTextStyle, text: label)
15381551
..textDirection = textDirection
1539-
..textScaleFactor = textScaleFactor
1540-
..layout();
1552+
..textScaler = TextScaler.linear(textScaleFactor)
1553+
..maxLines = 1
1554+
..ellipsis =
1555+
'\u2026' // Standard Unicode ellipsis
1556+
..layout(
1557+
maxWidth: screenSize.width.isFinite && screenSize.width > 0
1558+
? safeMaxWidth
1559+
: double.infinity,
1560+
);
15411561
} else {
15421562
_labelPainter.text = null;
15431563
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Constrains Slider and RangeSlider value indicator labels to the screen width, truncating with an ellipsis instead of clipping.
3+
version: patch

‎packages/material_ui/test/range_slider_test.dart‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4073,13 +4073,56 @@ void main() {
40734073
expect(endFocusNode.hasFocus, isTrue, reason: 'End thumb should have focus after tab');
40744074
expect(FocusManager.instance.primaryFocus, equals(endFocusNode));
40754075
});
4076+
4077+
testWidgets('RangeSlider labels respect horizontal buffer and avoid screen overflow', (
4078+
WidgetTester tester,
4079+
) async {
4080+
final logPainters = <TextPainter>[];
4081+
final shape = LoggingRangeSliderValueIndicatorShape(<InlineSpan>[], logPainters);
4082+
4083+
const longLabelStart = 'A very long start label string that exceeds standard screen widths';
4084+
const longLabelEnd = 'A very long end label string that exceeds standard screen widths';
4085+
4086+
await tester.pumpWidget(
4087+
MaterialApp(
4088+
home: Scaffold(
4089+
body: SliderTheme(
4090+
data: SliderThemeData(
4091+
showValueIndicator: ShowValueIndicator.always,
4092+
rangeValueIndicatorShape: shape,
4093+
),
4094+
child: RangeSlider(
4095+
values: const RangeValues(0.2, 0.8),
4096+
divisions: 10,
4097+
labels: const RangeLabels(longLabelStart, longLabelEnd),
4098+
onChanged: (RangeValues values) {},
4099+
),
4100+
),
4101+
),
4102+
),
4103+
);
4104+
4105+
// Press the range slider to trigger the value indicators
4106+
final TestGesture gesture = await tester.startGesture(
4107+
tester.getCenter(find.byType(RangeSlider)),
4108+
);
4109+
await tester.pumpAndSettle();
4110+
4111+
final double screenWidth = tester.view.physicalSize.width / tester.view.devicePixelRatio;
4112+
4113+
expect(logPainters, isNotEmpty);
4114+
expect(logPainters.last.width, lessThanOrEqualTo(screenWidth - 64.0));
4115+
4116+
await gesture.up();
4117+
});
40764118
}
40774119

40784120
// A value indicator shape to log labelPainter text.
40794121
class LoggingRangeSliderValueIndicatorShape extends RangeSliderValueIndicatorShape {
4080-
LoggingRangeSliderValueIndicatorShape(this.logLabel);
4122+
LoggingRangeSliderValueIndicatorShape(this.logLabel, [this.logPainter]);
40814123

40824124
final List<InlineSpan> logLabel;
4125+
final List<TextPainter>? logPainter;
40834126

40844127
@override
40854128
Size getPreferredSize(
@@ -4109,5 +4152,6 @@ class LoggingRangeSliderValueIndicatorShape extends RangeSliderValueIndicatorSha
41094152
Thumb? thumb,
41104153
}) {
41114154
logLabel.add(labelPainter.text!);
4155+
logPainter?.add(labelPainter);
41124156
}
41134157
}

‎packages/material_ui/test/slider_test.dart‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ class LoggingThumbShape extends SliderComponentShape {
103103

104104
// A value indicator shape to log labelPainter text.
105105
class LoggingValueIndicatorShape extends SliderComponentShape {
106-
LoggingValueIndicatorShape(this.logLabel);
106+
LoggingValueIndicatorShape(this.logLabel, [this.logPainter]);
107107

108108
final List<InlineSpan> logLabel;
109+
final List<TextPainter>? logPainter;
109110

110111
@override
111112
Size getPreferredSize(bool isEnabled, bool isDiscrete) {
@@ -128,6 +129,7 @@ class LoggingValueIndicatorShape extends SliderComponentShape {
128129
required Size sizeWithOverflow,
129130
}) {
130131
logLabel.add(labelPainter.text!);
132+
logPainter?.add(labelPainter);
131133
}
132134
}
133135

@@ -5673,6 +5675,43 @@ void main() {
56735675
);
56745676
expect(tester.getSize(find.byType(Slider)), Size.zero);
56755677
});
5678+
5679+
testWidgets('Slider label respects horizontal buffer and avoids screen overflow', (
5680+
WidgetTester tester,
5681+
) async {
5682+
final logPainters = <TextPainter>[];
5683+
final shape = LoggingValueIndicatorShape(<InlineSpan>[], logPainters);
5684+
5685+
await tester.pumpWidget(
5686+
MaterialApp(
5687+
home: Scaffold(
5688+
body: SliderTheme(
5689+
data: SliderThemeData(
5690+
showValueIndicator: ShowValueIndicator.always,
5691+
valueIndicatorShape: shape,
5692+
),
5693+
child: Slider(
5694+
value: 0.5,
5695+
divisions: 10,
5696+
label:
5697+
'A very long label string that exceeds standard screen widths to test clipping behavior',
5698+
onChanged: (double value) {},
5699+
),
5700+
),
5701+
),
5702+
),
5703+
);
5704+
5705+
final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(Slider)));
5706+
await tester.pumpAndSettle();
5707+
5708+
final double screenWidth = tester.view.physicalSize.width / tester.view.devicePixelRatio;
5709+
5710+
expect(logPainters, isNotEmpty);
5711+
expect(logPainters.last.width, lessThanOrEqualTo(screenWidth - 64.0));
5712+
5713+
await gesture.up();
5714+
});
56765715
}
56775716

56785717
// A slider value indicator that's a circle with a fixed size and

0 commit comments

Comments
 (0)