An immutable span of text.

A TextSpan object can be styled using its style property. The style will be applied to the text and the children.

A TextSpan object can just have plain text, or it can have children TextSpan objects with their own styles that (possibly only partially) override the style of this object. If a TextSpan has both text and children, then the text is treated as if it was an unstyled TextSpan at the start of the children list.

To paint a TextSpan on a Canvas, use a TextPainter. To display a text span in a widget, use a RichText. For text with a single style, consider using the Text widget.

Sample code

The text "Hello world!", in black:

new TextSpan(
  text: 'Hello world!',
  style: new TextStyle(color: Colors.black),
)

There is some more detailed sample code in the documentation for the recognizer property.

See also:

Annotations
  • @immutable

Constructors

TextSpan({TextStyle style, String text, List<TextSpan> children, GestureRecognizer recognizer })
Creates a TextSpan with the given values. [...]
const

Properties

children List<TextSpan>
Additional spans to include as children. [...]
final
hashCode int
@override, read-only
recognizer GestureRecognizer
A gesture recognizer that will receive events that hit this text span. [...]
final
style TextStyle
The style to apply to the text and the children.
final
text String
The text contained in the span. [...]
final
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

build(ParagraphBuilder builder, { double textScaleFactor: 1.0 }) → void
Apply the style, text, and children of this object to the given ParagraphBuilder, from which a Paragraph can be obtained. Paragraph objects can be drawn on Canvas objects. [...]
codeUnitAt(int index) int
Returns the UTF-16 code unit at the given index in the flattened string. [...]
debugAssertIsValid() bool
In checked mode, throws an exception if the object is not in a valid configuration. Otherwise, returns true. [...]
getSpanForPosition(TextPosition position) TextSpan
Returns the text span that contains the given position in the text.
toPlainText() String
Flattens the TextSpan tree into a single string. [...]
toString([String prefix = '' ]) String
Returns a string representation of this object.
visitTextSpan(bool visitor(TextSpan span)) bool
Walks this text span and its decendants in pre-order and calls visitor for each span that has text.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited

Operators

operator ==(other) bool
The equality operator. [...]