TextSpan class

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 un-styled TextSpan at the start of the children list. Leaving the TextSpan.text field null results in the TextSpan acting as an empty node in the InlineSpan tree with a list of children.

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.

The text "Hello world!", in black:
link
const TextSpan(
  text: 'Hello world!',
  style: TextStyle(color: Colors.black),
)

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

The TextSpan.text will be used as the semantics label unless overridden by the TextSpan.semanticsLabel property. Any PlaceholderSpans in the TextSpan.children list will separate the text before and after it into two semantics nodes.

See also:

  • WidgetSpan, a leaf node that represents an embedded inline widget in an InlineSpan tree. Specify a widget within the children list by wrapping the widget with a WidgetSpan. The widget will be laid out inline within the paragraph.
  • Text, a widget for showing uniformly-styled text.
  • RichText, a widget for finer control of text rendering.
  • TextPainter, a class for painting TextSpan objects on a Canvas.
Inheritance
Implemented types
Annotations

Constructors

TextSpan({String? text, List<InlineSpan>? children, TextStyle? style, GestureRecognizer? recognizer, MouseCursor? mouseCursor, PointerEnterEventListener? onEnter, PointerExitEventListener? onExit, String? semanticsLabel, Locale? locale, bool? spellOut})
Creates a TextSpan with the given values.
const

Properties

children List<InlineSpan>?
Additional spans to include as children.
final
cursor MouseCursor
Returns the value of mouseCursor.
no setteroverride
hashCode int
The hash code for this object.
no setteroverride
locale Locale?
The language of the text in this span and its span children.
final
mouseCursor MouseCursor
Mouse cursor when the mouse hovers over this span.
final
onEnter PointerEnterEventListener?
Triggered when a mouse pointer, with or without buttons pressed, has entered the region and validForMouseTracker is true.
final
onExit PointerExitEventListener?
Triggered when a mouse pointer, with or without buttons pressed, has exited the region and validForMouseTracker is true.
final
recognizer GestureRecognizer?
A gesture recognizer that will receive events that hit this span.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
semanticsLabel String?
An alternative semantics label for this TextSpan.
final
spellOut bool?
Whether the assistive technologies should spell out this text character by character.
final
style TextStyle?
The TextStyle to apply to this span.
finalinherited
text String?
The text contained in this span.
final
validForMouseTracker bool
Whether this is included when MouseTracker collects the list of annotations.
no setteroverride

Methods

build(ParagraphBuilder builder, {TextScaler textScaler = TextScaler.noScaling, List<PlaceholderDimensions>? dimensions}) → 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.
override
codeUnitAt(int index) int?
Returns the UTF-16 code unit at the given index in the flattened string.
inherited
codeUnitAtVisitor(int index, Accumulator offset) int?
Performs the check at each InlineSpan for if the index falls within the range of the span and returns the corresponding code unit. Returns null otherwise.
override
compareTo(InlineSpan other) RenderComparison
Describe the difference between this span and another, in terms of how much damage it will make to the rendering. The comparison is deep.
override
computeSemanticsInformation(List<InlineSpanSemanticsInformation> collector, {Locale? inheritedLocale, bool inheritedSpellOut = false}) → void
Walks the InlineSpan tree and accumulates a list of InlineSpanSemanticsInformation objects.
override
computeToPlainText(StringBuffer buffer, {bool includeSemanticsLabels = true, bool includePlaceholders = true}) → void
Walks the InlineSpan tree and writes the plain text representation to buffer.
override
debugAssertIsValid() bool
In debug mode, throws an exception if the object is not in a valid configuration. Otherwise, returns true.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
override
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
override
getSemanticsInformation() List<InlineSpanSemanticsInformation>
Flattens the InlineSpan tree to a list of InlineSpanSemanticsInformation objects.
inherited
getSpanForPosition(TextPosition position) InlineSpan?
Returns the InlineSpan that contains the given position in the text.
inherited
getSpanForPositionVisitor(TextPosition position, Accumulator offset) InlineSpan?
Returns the text span that contains the given position in the text.
override
handleEvent(PointerEvent event, HitTestEntry<HitTestTarget> entry) → void
Override this method to receive events.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toPlainText({bool includeSemanticsLabels = true, bool includePlaceholders = true}) String
Flattens the InlineSpan tree into a single string.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A brief description of this object, usually just the runtimeType and the hashCode.
override
visitChildren(InlineSpanVisitor visitor) bool
Walks this TextSpan and its descendants in pre-order and calls visitor for each span that has text.
override
visitDirectChildren(InlineSpanVisitor visitor) bool
Calls visitor for each immediate child of this InlineSpan.
override

Operators

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