Flutter Cursor Position on Text Field
1,093
I fixed my problem with this;
// Get cursor current position
var cursorPos =
_textEditController.selection.base.offset;
// Right text of cursor position
String suffixText =
_textEditController.text.substring(cursorPos);
// Add new text on cursor position
String specialChars = ' text_1 ';
int length = specialChars.length;
// Get the left text of cursor
String prefixText =
_textEditController.text.substring(0, cursorPos);
_textEditController.text =
prefixText + specialChars + suffixText;
// Cursor move to end of added text
_textEditController.selection = TextSelection(
baseOffset: cursorPos + length,
extentOffset: cursorPos + length,
);

Author by
secret
Updated on December 26, 2022Comments
-
secret 11 minutes
I want to append text after the cursor position. For example: when the user moved the cursor of the text field middle. I want to get the cursor location on the text and I'll add text after the cursor location.
I tried with Text Edit Controller but I can't reach the cursor location with this. How can detect the cursor location on the text field?