You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter开发问题:TextFormField光标自动跳至开头,需定位至文本末尾

Fixing Cursor Jump to Start in Flutter TextFormField

Hey there! I see you're dealing with that annoying issue where tapping your TextFormField makes the cursor jump to the start instead of staying at the end of existing text. Let's get this sorted out quickly.

The Root Cause

This happens because when the field gains focus, Flutter's default behavior doesn't always position the cursor at the end of pre-filled text. We need to manually override this by setting the cursor position explicitly when the field is focused.

This approach gives you more control over focus state and works reliably:

  1. First, create a FocusNode in your state class:
FocusNode _titleFocusNode = FocusNode();
  1. Add a listener to the FocusNode in initState to set the cursor position when the field gains focus:
@override
void initState() {
  super.initState();
  _titleFocusNode.addListener(() {
    if (_titleFocusNode.hasFocus) {
      // Move cursor to the end of the text
      titleContr.selection = TextSelection.fromPosition(
        TextPosition(offset: titleContr.text.length)
      );
    }
  });
}
  1. Attach the FocusNode to your TextFormField, and don't forget to dispose it later:
Padding(
  padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
  child: TextFormField(
    controller: titleContr,
    focusNode: _titleFocusNode, // Add this line
    style: textStyle,
    validator: (String value) {
      if (value.isEmpty) {
        return 'Please enter a title';
      }
    },
    decoration: InputDecoration(
      labelText: 'Title',
      labelStyle: textStyle,
      hintText: 'Add a title for the Task',
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(5.0)
      )
    ),
  ),
)
  1. Dispose the FocusNode and TextEditingController in dispose to prevent memory leaks:
@override
void dispose() {
  _titleFocusNode.dispose();
  titleContr.dispose();
  super.dispose();
}

Solution 2: Quick Fix with onTap Callback

If you want a simpler, one-off solution without a FocusNode, you can use the onTap callback with addPostFrameCallback (this ensures we override Flutter's default cursor positioning):

Padding(
  padding: EdgeInsets.only(top: 15.0, bottom: 15.0),
  child: TextFormField(
    controller: titleContr,
    style: textStyle,
    onTap: () {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        titleContr.selection = TextSelection.fromPosition(
          TextPosition(offset: titleContr.text.length)
        );
      });
    },
    validator: (String value) {
      if (value.isEmpty) {
        return 'Please enter a title';
      }
    },
    decoration: InputDecoration(
      labelText: 'Title',
      labelStyle: textStyle,
      hintText: 'Add a title for the Task',
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(5.0)
      )
    ),
  ),
)

Either of these solutions will make sure the cursor stays at the end of your existing text when you tap the field. Test it out, and it should work like a charm!

内容的提问来源于stack exchange,提问作者R. Gurung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:59:46