首页 > 解决方案 > Flutter 设置 TextField 光标位置

问题描述

我的应用程序中有一个简单的TextField小部件。我注意到蓝色光标始终位于末尾,以便我可以在那里输入更多文本。但是,当我在输入的开头或中间的任何地方点击以更改光标焦点位置时,它不会去那里。它总是在那里,我必须删除所有输入才能回到那里。

在此处输入图像描述

我已经搜索了如何实现这一点,但我发现最接近的是这个我不明白的小例子。

https://www.codegrepper.com/code-examples/whatever/flutter+texteditingcontroller+cursor+position

以下是我的小部件。

TextField(
                          controller: _descriptionTextController,
                          enableInteractiveSelection: false,
                          keyboardType: TextInputType.multiline,
                          maxLines: null,
                          decoration: InputDecoration(
                            fillColor: Colors.transparent,
                            border: InputBorder.none,
                            focusedBorder: InputBorder.none,
                            enabledBorder: InputBorder.none,
                            errorBorder: InputBorder.none,
                            disabledBorder: InputBorder.none,
                            hintText: 'Product description',
                          ),
                          style: TextFieldStyle,
                        ),

谁能帮帮我。

标签: flutter

解决方案


允许其他交互以及文本插入符号的移动

您想要做的是能够在用户与文本交互时移动文本插入符号。您已设置enableInteractiveSelection为假。

文档

如果为 false,则用户无法调整文本选择,无法复制文本,并且用户无法从剪贴板粘贴到文本字段中。

只允许移动文本插入符号

如果您想禁用剪切/复制/粘贴操作,我建议您查看ToolbarOptions


推荐阅读