AttributedText class

Text with attributions applied to desired spans of text.

An attribution can be any subclass of Attribution.

AttributedText is a convenient way to store and manipulate text that might have overlapping styles and/or non-style attributions. A common Flutter alternative is TextSpan, but TextSpan does not support overlapping styles, and TextSpan is exclusively intended for visual text styles.

Available extensions

Constructors

AttributedText([String? text, AttributedSpans? spans, Map<int, Object>? placeholders])
Constructs an AttributedText whose content is comprised by a combination of text and placeholders, covered by the given attributed spans.

Properties

hashCode int
The hash code for this object.
no setteroverride
hasListeners bool
no setter
isEmpty bool
Returns true if the length of this AttributedText is zero.
no setter
isNotEmpty bool
Returns true if the length of this AttributedText is greater than zero.
no setter
length int
Returns the length of this AttributedText, which includes the length of the plain text String, and the number of placeholders.
no setter
placeholders Map<int, Object>
Placeholders that represent non-text content, e.g., inline images, that should appear in the rendered text.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
spans AttributedSpans
The attributes applied across the plain text and placeholders.
final
text String
The text that this AttributedText attributes.
no setter

Methods

addAttribution(Attribution attribution, SpanRange range, {bool autoMerge = true, bool overwriteConflictingSpans = false}) → void
Adds the given attribution to all characters within the given range, inclusive.
addListener(VoidCallback listener) → void
clearAttributions(SpanRange range) → void
Removes all attributions within the given range.
computeAttributionSpans() Iterable<MultiAttributionSpan>
Collapses all attribution markers down into a series of attribution groups, starting at the beginning of this AttributedText, until the end.
computeInlineSpan(BuildContext context, AttributionStyleBuilder styleBuilder, InlineWidgetBuilderChain inlineWidgetBuilders) InlineSpan

Available on AttributedText, provided by the ComputeTextSpan extension

Returns a Flutter InlineSpan comprised of styled text and widgets based on an AttributedText.
computeTextSpan(AttributionStyleBuilder styleBuilder) TextSpan

Available on AttributedText, provided by the ComputeTextSpan extension

Returns a Flutter TextSpan that is styled based on the attributions within this AttributedText.
copy() AttributedText
Returns a copy of this AttributedText.
copyAndAppend(AttributedText other) AttributedText
Returns a copy of this AttributedText with the other text and attributions appended to the end.
copyText(int startOffset, [int? endOffset]) AttributedText
Copies all text, attributions, and placeholders from startOffset to endOffset, exclusive, and returns them as a new AttributedText.
copyTextInRange(SpanRange range) AttributedText
Copies all text and attributions from range.start to range.end (exclusive), and returns them as a new AttributedText.
dispose() → void
getAllAttributionsAt(int offset) Set<Attribution>
Returns all attributions applied to the given offset.
getAllAttributionsThroughout(SpanRange range) Set<Attribution>
Returns all attributions that appear throughout the entirety of the given range.
getAttributedRange(Set<Attribution> attributions, int offset) SpanRange
Returns the range about offset, which is attributed with all given attributions.
getAttributionSpans(Set<Attribution> attributions) Set<AttributionSpan>
Returns all spans in this AttributedText for the given attributions.
getAttributionSpansByFilter(AttributionFilter filter) Set<AttributionSpan>
Returns all spans in this AttributedText, for attributions that are selected by the given filter.
getAttributionSpansInRange({required AttributionFilter attributionFilter, required SpanRange range, bool resizeSpansToFitInRange = false}) Set<AttributionSpan>
Returns spans for each attribution that (at least partially) appear within the given range, as selected by attributionFilter.
hasAttributionAt(int offset, {Attribution? attribution}) bool
Returns true if the given attribution is applied at offset.
hasAttributionsThroughout({required Set<Attribution> attributions, required SpanRange range}) bool
Returns true if this AttributedText contains each of the given attributions throughout the given range (inclusive).
hasAttributionsWithin({required Set<Attribution> attributions, required SpanRange range}) bool
Returns true if this AttributedText contains at least one character with each of the given attributions within the given range (inclusive).
insert({required AttributedText textToInsert, required int startOffset}) AttributedText
Returns a copy of this AttributedText with textToInsert inserted at startOffset, retaining whatever attributions are already applied to textToInsert.
insertPlaceholder(int index, Object placeholder) AttributedText
insertPlaceholders(Map<int, Object> placeholders) AttributedText
insertString({required String textToInsert, required int startOffset, Set<Attribution> applyAttributions = const {}}) AttributedText
Returns a copy of this AttributedText with textToInsert inserted at startOffset.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
removeAttribution(Attribution attribution, SpanRange range) → void
Removes the given attribution from all characters within the given range, inclusive.
removeListener(VoidCallback listener) → void
removeRegion({required int startOffset, required int endOffset}) AttributedText
Copies this AttributedText and removes a region of text and attributions from startOffset, inclusive, to endOffset, exclusive.
replaceAttributions(AttributedSpans newSpans) AttributedText
Returns a copy of this AttributedText, replacing the existing AttributedSpans with the given newSpans.
substring(int start, [int? end]) String
Returns a plain-text substring, from start to end (exclusive), or the end of this AttributedText if end isn't provided.
substringInRange(SpanRange range) String
Returns a plain-text substring, from range.start to range.end (exclusive).
toggleAttribution(Attribution attribution, SpanRange range) → void
If ALL of the text in range, inclusive, contains the given attribution, that attribution is removed from the text in range, inclusive. Otherwise, all of the text in range, inclusive, is given the attribution.
toPlainText({bool includePlaceholders = true, String replacementCharacter = placeholderCharacter}) String
Returns a plain-text version of this AttributedText.
toString() String
A string representation of this object.
override
visitAttributions(AttributionVisitor visitor) → void
Visits all attributions in this AttributedText by calling visitor whenever an attribution begins or ends.
visitAttributionSpans(AttributionSpanVisitor visitor) → void
Visits attributions in this AttributedText, reporting every changing group of attributions to the given visitor.

Operators

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

Constants

placeholderCharacter → const String
The default character that's inserted in place of placeholders when converting an AttributedText to plain text.