AttributedText constructor
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
.
placeholders
is a map from character indices to desired placeholder objects.
The character indices in placeholders
refer to the final indices when the
placeholders have been combined with the text
.
Example:
- Full text: "�Hello � World!�"
- text: "Hello World!"
- placeholders:
- 0: MyPlaceholder
- 7: MyPlaceholder
- 15: MyPlaceholder
Notice in the example above that the final placeholder index is greater
than the total length of the text
String
.
Implementation
AttributedText([
String? text,
AttributedSpans? spans,
Map<int, Object>? placeholders,
]) : _text = text ?? "",
spans = spans ?? AttributedSpans(),
placeholders = placeholders ?? <int, Object>{} {
assert(() {
// ^ Run this in an assert with a callback so that the validation doesn't run in
// production and cost processor cycles.
_validatePlaceholderIndices();
return true;
}());
if (this.placeholders.isEmpty) {
// There aren't any placeholders, so text with placeholders is the same as
// text without placeholders.
_textWithPlaceholders = _text;
} else {
// Create a 2nd plain text representation that includes stand-in characters
// for placeholders.
final buffer = StringBuffer();
int start = 0;
int insertedPlaceholders = 0;
for (final entry in this.placeholders.entries) {
final textSegment = _text.substring(start - insertedPlaceholders, entry.key - insertedPlaceholders);
buffer.write(textSegment);
start += textSegment.length;
buffer.write(placeholderCharacter);
start += 1;
insertedPlaceholders += 1;
}
if (start - insertedPlaceholders < _text.length) {
buffer.write(_text.substring(start - insertedPlaceholders, _text.length));
}
_textWithPlaceholders = buffer.toString();
}
}