operator + method Null safety

AttributedString operator +(
  1. AttributedString other
)

Returns a new AttributedString by concatenate the operands

The string attribute list of the returned AttributedString will contains the string attributes from both operands with updated text ranges.

Implementation

AttributedString operator +(AttributedString other) {
  if (string.isEmpty) {
    return other;
  }
  if (other.string.isEmpty) {
    return this;
  }

  // None of the strings is empty.
  final String newString = string + other.string;
  final List<StringAttribute> newAttributes = List<StringAttribute>.of(attributes);
  if (other.attributes.isNotEmpty) {
    final int offset = string.length;
    for (final StringAttribute attribute in other.attributes) {
      final TextRange newRange = TextRange(
        start: attribute.range.start + offset,
        end: attribute.range.end + offset,
      );
      final StringAttribute adjustedAttribute = attribute.copy(range: newRange);
      newAttributes.add(adjustedAttribute);
    }
  }
  return AttributedString(newString, attributes: newAttributes);
}