Invalidate the TextPainter line metrics cache when redoing text layout (#97446)

This commit is contained in:
Jason Simmons
2022-02-03 09:10:17 -08:00
committed by GitHub
parent 6aed693c4c
commit 93a1177770
3 changed files with 23 additions and 2 deletions

View File

@@ -648,7 +648,8 @@ class TextPainter {
_createParagraph();
_lastMinWidth = minWidth;
_lastMaxWidth = maxWidth;
// A change in layout invalidates the cached caret metrics as well.
// A change in layout invalidates the cached caret and line metrics as well.
_lineMetricsCache = null;
_previousCaretPosition = null;
_previousCaretPrototype = null;
_layoutParagraph(minWidth, maxWidth);

View File

@@ -2416,7 +2416,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin,
// ui.LineMetrics, then we can get rid of this.
final Offset offset = _textPainter.getOffsetForCaret(startPosition, Rect.zero);
for (final ui.LineMetrics lineMetrics in metrics) {
if (lineMetrics.baseline + lineMetrics.descent > offset.dy) {
if (lineMetrics.baseline > offset.dy) {
return MapEntry<int, Offset>(lineMetrics.lineNumber, Offset(offset.dx, lineMetrics.baseline));
}
}

View File

@@ -996,6 +996,26 @@ void main() {
ui.Rect.zero);
expect(caretOffset.dx, painter.width);
}, skip: kIsWeb && !isCanvasKit); // https://github.com/flutter/flutter/issues/87545
test('TextPainter line metrics update after layout', () {
final TextPainter painter = TextPainter()
..textDirection = TextDirection.ltr;
const String text = 'word1 word2 word3';
painter.text = const TextSpan(
text: text,
);
painter.layout(maxWidth: 80);
List<ui.LineMetrics> lines = painter.computeLineMetrics();
expect(lines.length, 3);
painter.layout(maxWidth: 1000);
lines = painter.computeLineMetrics();
expect(lines.length, 1);
}, skip: kIsWeb && !isCanvasKit); // https://github.com/flutter/flutter/issues/62819
}
class MockCanvas extends Fake implements Canvas {