skip to Main Content

Hi I have a problem moving cursor to the end of the text inside text form field while using RTL languages like Farsi. When I move the cursor to the end (position n), suddenly it jumps to one character before the end (n-1).
I’ve searched every where and found similar issue here (Flutter textfield Flutter RTL cursor position problem n-1) but the solution didn’t work.
I have attached a video of the problem.enter image description here

2

Answers


  1. Put this code in onTap function in TextField:

    onTap: () {
      if (controller != null) {
        if (controller!.selection == TextSelection.fromPosition(TextPosition(offset: controller!.text.length - 1))) {
          controller!.selection = TextSelection.fromPosition(TextPosition(offset: controller!.text.length));
        }
      }
    },
    
    Login or Signup to reply.
  2. You can fix this by registering a listener and watch over the cursor position.

    @override
    void initState() {
      super.initState();
      _textEditingController.addListener(() {
        if (_textEditingController != null) {
          if (_textEditingController.selection == TextSelection.fromPosition(TextPosition(offset: _textEditingController.text.length - 1))) {
            _textEditingController.selection = TextSelection.fromPosition(
              TextPosition(
                offset: _textEditingController.text.length,
              ),
            );
          }
        }
      });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search