Flutter – How to set TextField cursor position

When working with TextField, if we use the controller to set new text for our TextField, the cursor will jump to the beginning of the text. This is not good behavior for the user to continue editing the text.

This post is a quick note on how to set textfield cursor position to the end of the text, using TextSelection:

// Assume you have a TextField with a controller
TextField(
  ...
  controller: controller,
  ...
)

// Set new text
controller.text = newText; 
// Change the cursor position
controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));
Tagged : / / / /
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x