TextPainter class Null safety

An object that paints a TextSpan tree into a Canvas.

To use a TextPainter, follow these steps:

  1. Create a TextSpan tree and pass it to the TextPainter constructor.

  2. Call layout to prepare the paragraph.

  3. Call paint as often as desired to paint the paragraph.

If the width of the area into which the text is being painted changes, return to step 2. If the text to be painted changes, return to step 1.

The default text style is white. To change the color of the text, pass a TextStyle object to the TextSpan in text.


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, TextHeightBehavior? textHeightBehavior})
Creates a text painter that paints the given text.


didExceedMaxLines bool
Whether any text was truncated or ellipsized.
ellipsis String?
The string used to ellipsize overflowing text. Setting this to a non-empty string will cause this string to be substituted for the remaining text if the text can not fit within the specified maximum width.
read / write
hashCode int
The hash code for this object.
read-only, inherited
height double
The vertical space required to paint this text.
inlinePlaceholderBoxes List<TextBox>?
An ordered list of TextBoxes that bound the positions of the placeholders in the paragraph.
inlinePlaceholderScales List<double>?
An ordered list of scales for each placeholder in the paragraph.
locale Locale?
The locale used to select region-specific glyphs.
read / write
maxIntrinsicWidth double
The width at which increasing the width of the text no longer decreases the height.
maxLines int?
An optional maximum number of lines for the text to span, wrapping if necessary.
read / write
minIntrinsicWidth double
The width at which decreasing the width of the text would prevent it from painting itself completely within its bounds.
preferredLineHeight double
The height of a space in text in logical pixels.
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
size Size
The amount of space required to paint this text.
strutStyle StrutStyle?
The strut style to use. Strut style defines the strut, which sets minimum vertical layout metrics.
read / write
text InlineSpan?
The (potentially styled) text to paint.
read / write
textAlign TextAlign
How the text should be aligned horizontally.
read / write
textDirection TextDirection?
The default directionality of the text.
read / write
textHeightBehavior TextHeightBehavior?
Defines how to apply TextStyle.height over and under text.
read / write
textScaleFactor double
The number of font pixels for each logical pixel.
read / write
textWidthBasis TextWidthBasis
Defines how to measure the width of the rendered text.
read / write
width double
The horizontal space required to paint this text.


computeDistanceToActualBaseline(TextBaseline baseline) double
Returns the distance from the top of the text to the first baseline of the given type.
computeLineMetrics() List<LineMetrics>
Returns the full list of LineMetrics that describe in detail the various metrics of each laid out line.
getBoxesForSelection(TextSelection selection, {BoxHeightStyle boxHeightStyle = ui.BoxHeightStyle.tight, BoxWidthStyle boxWidthStyle = ui.BoxWidthStyle.tight}) List<TextBox>
Returns a list of rects that bound the given selection.
getFullHeightForCaret(TextPosition position, Rect caretPrototype) double?
Returns the strut bounded height of the glyph at the given position.
getLineBoundary(TextPosition position) TextRange
Returns the text range of the line at the given offset.
getOffsetAfter(int offset) int?
Returns the closest offset after offset at which the input cursor can be positioned.
getOffsetBefore(int offset) int?
Returns the closest offset before offset at which the input cursor can be positioned.
getOffsetForCaret(TextPosition position, Rect caretPrototype) Offset
Returns the offset at which to paint the caret.
getPositionForOffset(Offset offset) TextPosition
Returns the position within the text for the given pixel offset.
getWordBoundary(TextPosition position) TextRange
Returns the text range of the word at the given offset. Characters not part of a word, such as spaces, symbols, and punctuation, have word breaks on both sides. In such cases, this method will return a text range that contains the given text position.
layout({double minWidth = 0.0, double maxWidth = double.infinity}) → void
Computes the visual position of the glyphs for painting the text.
markNeedsLayout() → void
Marks this text painter's layout information as dirty and removes cached information.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
paint(Canvas canvas, Offset offset) → void
Paints the text onto the given canvas at the given offset.
setPlaceholderDimensions(List<PlaceholderDimensions>? value) → void
Sets the dimensions of each placeholder in text.
toString() String
A string representation of this object.


operator ==(Object other) bool
The equality operator.