0

当我重新编辑我的 TextField 时,为什么光标显示在现有文本的开头,而不是用户点击的位置。当您取消焦点文本字段然后再次重新关注它时,会发生这种情况。

示例代码:

class Mypage extends StatelessWidget {
  const Mypage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextField(
              controller: TextEditingController()..text = 'Existing Text Here',
            ),
            TextButton(
                onPressed: () {
                  FocusScope.of(context).unfocus();
                },
                child: Text('Unfocus'))
          ],
        ),
      ),
    );
  }
}
4

1 回答 1

0

默认情况下,使用 aTextEditingController而不设置selection属性,将位于第一个字符/文本值。你可以尝试这样的事情:

String textValue = 'Existing Text Here';

TextField(
  controller: TextEditingController()..value = TextEditingValue(
  text: textValue,
  selection: TextSelection.fromPosition(
  TextPosition(offset: textValue.length))
    )
  ),
于 2021-09-04T02:24:07.657 回答