Ebrahim Halabi
Ebrahim Halabi

Reputation: 83

Flutter textfield Flutter RTL cursor position problem n-1

I don't know if the problem is old but I tried all ways to find a solution without success. the problem when select letter n in input field the cursor jump to n-1 the problem shown in video included , problem in rtl only

The link contains the code and a video of the problem.

the code : link here

the video : link here

Upvotes: 8

Views: 1008

Answers (2)

Erfan Ghasemi
Erfan Ghasemi

Reputation: 134

The answer that has been submitted as correct answer, does not work on cursor movement. It only works on tap.

You can fix this by registering a listener on text editing controller 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,
                      ),
                    );
                  }
                }
              }
            );
}

Upvotes: 1

Omar Alkattan
Omar Alkattan

Reputation: 518

I think this because of some characters is encoded as 2 to 4 bytes.

Anyway this snippet can do the trick

TextField(
     onTap: (){
          if(controller.selection == TextSelection.fromPosition(TextPosition(offset: controller.text.length -1))){
             setState(() {
                 controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));
             });
           }
     },
     controller: controller,
     ...
);

Upvotes: 4

Related Questions