Invalidate the TextPainter line metrics cache when redoing text layout (#97446)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user