1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| class TextPainter { TextPainter({ InlineSpan? text, TextAlign textAlign = TextAlign.start, TextDirection? textDirection, double textScaleFactor = 1.0, int? maxLines, String? ellipsis, Locale? locale, StrutStyle? strutStyle, TextWidthBasis textWidthBasis = TextWidthBasis.parent, ui.TextHeightBehavior? textHeightBehavior, })
void layout({ double minWidth = 0.0, double maxWidth = double.infinity }) { if (_paragraph != null && minWidth == _lastMinWidth && maxWidth == _lastMaxWidth) { return; }
if (_rebuildParagraphForPaint || _paragraph == null) { _createParagraph(); } _lastMinWidth = minWidth; _lastMaxWidth = maxWidth; _lineMetricsCache = null; _previousCaretPosition = null; _layoutParagraph(minWidth, maxWidth); _inlinePlaceholderBoxes = _paragraph!.getBoxesForPlaceholders(); }
void _layoutParagraph(double minWidth, double maxWidth) { _paragraph!.layout(ui.ParagraphConstraints(width: maxWidth)); if (minWidth != maxWidth) { double newWidth; switch (textWidthBasis) { case TextWidthBasis.longestLine: newWidth = _applyFloatingPointHack(_paragraph!.longestLine); case TextWidthBasis.parent: newWidth = maxIntrinsicWidth; } newWidth = clampDouble(newWidth, minWidth, maxWidth); if (newWidth != _applyFloatingPointHack(_paragraph!.width)) { _paragraph!.layout(ui.ParagraphConstraints(width: newWidth)); } } }
void paint(Canvas canvas, Offset offset) { final double? minWidth = _lastMinWidth; final double? maxWidth = _lastMaxWidth; if (_paragraph == null || minWidth == null || maxWidth == null) { throw StateError( 'TextPainter.paint called when text geometry was not yet calculated.\n' 'Please call layout() before paint() to position the text before painting it.', ); }
if (_rebuildParagraphForPaint) { _createParagraph(); _layoutParagraph(minWidth, maxWidth); } canvas.drawParagraph(_paragraph!, offset); }
ui.Paragraph? _paragraph;
ui.Paragraph _createParagraph() { final InlineSpan? text = this.text; if (text == null) { throw StateError('TextPainter.text must be set to a non-null value before using the TextPainter.'); } final ui.ParagraphBuilder builder = ui.ParagraphBuilder(_createParagraphStyle()); text.build(builder, textScaleFactor: textScaleFactor, dimensions: _placeholderDimensions); _inlinePlaceholderScales = builder.placeholderScales; final ui.Paragraph paragraph = _paragraph = builder.build(); _rebuildParagraphForPaint = false; return paragraph; }
}
|