insert static method

TextEditingValue insert(
  1. TextEditingValue value,
  2. String insert
)

Inserts insert in place of the selection, with a space before it when the character before isn't whitespace (so a Mention button's @ starts a new word).

Implementation

static TextEditingValue insert(TextEditingValue value, String insert) {
  final TextSelection sel = selectionOf(value);
  final String pre =
      sel.start > 0 && !_whitespace.hasMatch(value.text[sel.start - 1])
      ? ' '
      : '';
  return TextEditingValue(
    text: value.text.replaceRange(sel.start, sel.end, '$pre$insert'),
    selection: TextSelection.collapsed(
      offset: sel.start + pre.length + insert.length,
    ),
  );
}