insertTextAtCursorPos method

void insertTextAtCursorPos(
  1. String text, {
  2. bool moveCursorToEnd = true,
})

Implementation

void insertTextAtCursorPos(String text, {bool moveCursorToEnd = true}) {
  if (textController.selection.isValid) {
    textController.value = TextEditingValue(
      text: textController.selection.isCollapsed
          ? textController.text.substring(0, textController.selection.start) +
              text +
              textController.text.substring(
                  textController.selection.end, textController.text.length)
          : textController.text.replaceRange(
              textController.selection.start,
              textController.selection.end,
              text,
            ),
      selection: moveCursorToEnd
          ? TextSelection.collapsed(
              offset: textController.selection.start + 4)
          : textController.selection,
    );
  }
}