0

Why does the cursor shows up at the beginning of the existing text, instead of the position where the user taps, when I re-edit my TextField. This happens when you unfocus the textfield then refocus on it again.

Example code:

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'))
          ],
        ),
      ),
    );
  }
}
satoru_02
  • 63
  • 4

1 Answers1

0

It is by default, using a TextEditingController without setting the selection property, will be at the first character/text value. You can try something like this:

String textValue = 'Existing Text Here';

TextField(
  controller: TextEditingController()..value = TextEditingValue(
  text: textValue,
  selection: TextSelection.fromPosition(
  TextPosition(offset: textValue.length))
    )
  ),
snap
  • 175
  • 1
  • 12
  • I've tried this one before, but this is not what I'm looking. What I want is to show the cursor where the user taps, just like the first time you tap the TextField. Is there a way to reset the TextEditingController so that it acts like it's the first time you're editing it? – satoru_02 Sep 04 '21 at 02:32