replaceSelectionWithSpans method

void replaceSelectionWithSpans(
  1. List<InlineSpan> spans
)

Replaces the current selection with the given spans.

ChipSpans are inserted as chips (registered in the chip map), while TextSpans and their text descendants are inserted as plain text. If the selection is not valid the spans are appended at the end. Used to reconstruct pasted content produced by a ClipboardHandler.

Implementation

void replaceSelectionWithSpans(List<InlineSpan> spans) {
  final String text = value.text;
  final TextSelection selection = value.selection;
  final int start = selection.isValid ? selection.start : text.length;
  final int end = selection.isValid ? selection.end : text.length;
  final StringBuffer buffer = StringBuffer();
  buffer.write(text.substring(0, start));
  for (final span in spans) {
    _writeSpan(buffer, span);
  }
  final int newOffset = buffer.length;
  buffer.write(text.substring(end));
  super.value = TextEditingValue(
    text: buffer.toString(),
    selection: TextSelection.collapsed(offset: newOffset),
  );
  _updateText(buffer.toString());
}