Skip to content

Commit 62e78bf

Browse files
Improve TextPainter.layout caching (#118128)
Improves `TextPainter.layout` caching when only the input constraints change: - removes the double layout calls in `TextPainter._layoutParagraph`: now double layout is only needed when `TextAlign` is not left, and the input `maxWidth == double.infinity`. - skip calls to `ui.Paragraph.layout` when it's guaranteed that there's no soft line breaks before/after the layout call. This doesn't introduce new APIs but may slightly shift text rendered on screen. This reduces the number of `layout` calls but since shaping results are already cached so it only skips the relatively cheap line-breaking process when possible. 528 scuba failures but all of them seem reasonable.
1 parent 4d1c6a4 commit 62e78bf

File tree

5 files changed

+431
-170
lines changed

5 files changed

+431
-170
lines changed

packages/flutter/lib/src/material/range_slider.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,7 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
11291129
}
11301130

11311131
void _updateLabelPainter(Thumb thumb) {
1132+
final RangeLabels? labels = this.labels;
11321133
if (labels == null) {
11331134
return;
11341135
}
@@ -1137,25 +1138,21 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
11371138
final TextPainter labelPainter;
11381139
switch (thumb) {
11391140
case Thumb.start:
1140-
text = labels!.start;
1141+
text = labels.start;
11411142
labelPainter = _startLabelPainter;
11421143
case Thumb.end:
1143-
text = labels!.end;
1144+
text = labels.end;
11441145
labelPainter = _endLabelPainter;
11451146
}
11461147

1147-
if (labels != null) {
1148-
labelPainter
1149-
..text = TextSpan(
1150-
style: _sliderTheme.valueIndicatorTextStyle,
1151-
text: text,
1152-
)
1153-
..textDirection = textDirection
1154-
..textScaleFactor = textScaleFactor
1155-
..layout();
1156-
} else {
1157-
labelPainter.text = null;
1158-
}
1148+
labelPainter
1149+
..text = TextSpan(
1150+
style: _sliderTheme.valueIndicatorTextStyle,
1151+
text: text,
1152+
)
1153+
..textDirection = textDirection
1154+
..textScaleFactor = textScaleFactor
1155+
..layout();
11591156
// Changing the textDirection can result in the layout changing, because the
11601157
// bidi algorithm might line up the glyphs differently which can result in
11611158
// different ligatures, different shapes, etc. So we always markNeedsLayout.

0 commit comments

Comments
 (0)