文本字段上的颤动光标位置

sec*_*ret 12 flutter

我想在光标位置后附加文本。例如:当用户将光标移动到文本字段中间时。我想获取文本上的光标位置,然后在光标位置后添加文本。

我尝试使用文本编辑控制器,但无法到达光标位置。如何检测文本字段上的光标位置?

sec*_*ret 14

我用这个解决了我的问题;

// 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,
                    );
Run Code Online (Sandbox Code Playgroud)