xen*_*s92 2 keyboard textfield dart flutter
我想创建一个默认行为被禁用的视图TextField(即我不启用单击时的默认键盘)。我创建了自己的键盘,因为它允许我为我的应用程序执行特定操作。键盘工作良好并填充TextField.
我的问题
我无法管理cursor中的闪烁TextField。事实上,当我在键盘上打字时,它cursor会跟随我的文本,所以没问题。另一方面,如果我决定在文本中间手动单击,则cursor移动但输入的新字符不会到达 的位置cursor。
这个怎么做 ?
它是什么样子的 ?
我的代码
我的文本字段小部件:
InputAtom(
autofocus: true,
controller: controllerInput,
placeholder: "Placeholder",
fontSize: 35,
keyboard: TextInputType.none,
enableInteractiveSelection: true,
showCursor: true,
cursorColor: Colors.red,
enableSuggestions: false,
autocorrect: false,
),
Run Code Online (Sandbox Code Playgroud)
我的按钮键盘示例:
KeyboardButtonAtom.numeric({
Key? key,
required String text,
required TextEditingController controller,
}): super(
key: key,
text: text,
controller: controller,
onPressed: (){
// Here I add the number typed on the keyboard after
controller.text += text.toString();
// Here the cursor moves to the end of my controller.text
controller.selection = TextSelection.collapsed(offset: controller.text.length);
print(controller.selection.baseOffset);
}
);
Run Code Online (Sandbox Code Playgroud)
cursor当我在 my 中手动输入某个位置TextField以便能够从该位置添加我的号码时,如何检索该位置cursor?
编辑
在我的中KeyboardButtonAtom,我这样做并且有效。感谢@Kaushik Chandru,
这里的代码无论光标的位置如何,甚至手动放置在字符串中间,都可以工作
String textBeforeCursor = controller.text.substring(0, controller.selection.baseOffset);
String textAfterCursor = controller.text.substring(controller.selection.extentOffset);
controller.text = textBeforeCursor + text.toString() + textAfterCursor;
int cursorPosition = textBeforeCursor.length + 1;
controller.selection = TextSelection.collapsed(offset: cursorPosition);
Run Code Online (Sandbox Code Playgroud)
要获取光标的当前位置,您可以尝试
var cursorPos = _textEditController.selection.base.offset;
String textAfterCursor = _textEditController.text.substring(cursorPos);
String textBeforeCursor = _textEditController.text.substring(0, cursorPos);
_textEditController.text = textBeforeCursor + "someText" + textAfterCursor;
_textEditingController.selection = TextSelection.collapsed(offset: _textEditingController.text.length);
Run Code Online (Sandbox Code Playgroud)