natintosh / intl_phone_number_input

MIT License
164 stars 496 forks source link

Curser issue while formating numbers #366

Open Navil opened 1 year ago

Navil commented 1 year ago

Describe the bug On iOS, when typing a US formated number (there are probably more cases), the cursor jumps to the wrong position after formating.

Package version 0.7.2

Flutter version 3.3.10

To Reproduce Only reproducable on iOS Steps to reproduce the behavior:

  1. Open dialog
  2. Type US format number: (e.g. +1 650 555 0001)
  3. The cursor will jump to the wrong position after adding the ( )

Expected behavior The cursor should not jump.

Screenshots

https://user-images.githubusercontent.com/3436399/212307759-4f8f0142-b612-46e3-ba40-6cee0062ccfe.mp4

Targeted platforms (please complete the following information):

tewedaj commented 1 year ago

This just happened to our clients yesterday, can any one please address the issue

tewedaj commented 1 year ago

TextEditingController controller = TextEditingController(); InternationalPhoneNumberInput( inputBorder: InputBorder.none, maxLength: Platform.isAndroid? 12 : 20, onInputChanged: (PhoneNumber number) { userDetail.phoneNumber = number.phoneNumber.toString(); controller.selection = TextSelection.collapsed(offset: controller.text.length); }, textFieldController: controller, formatInput: true, )

can you try doing something like this? i don't have IOS on me right now.. try it and let me know if it worked for you

tewedaj commented 1 year ago

I was told the above code worked on iPhone 11 Pro max..

mingzew commented 1 year ago

Encountering the same issue, adding the below line helped me

InternationalPhoneNumberInput(
  ....
  onInputChanged: (PhoneNumber value) {
     _phoneNumberController.selection = TextSelection.collapsed(offset: _phoneNumberController.text.length);
  }
);
iabdousd commented 1 year ago

This is new on the new version, it's coming with the update to the PhoneNumberKit on iOS from libphonenumber-iOS

MahdiFakhrabadi commented 1 year ago

The issue is originating from utils/formatter/as_you_type_formatter.dart Line 86: TextEditingValue( text: parsedText, selection: TextSelection.collapsed(offset: parsedText.length), )

It is possible to solve it by replacing selection to this: selection: TextSelection.collapsed(offset: parsedText.length)

If this doesn't break somewhere else, please somebody commit this to codebase, thanks.

Shehzad1Dev commented 1 year ago

In onInputChanged method, add the following line

numCont.selection = TextSelection.collapsed(offset: numCont.text.length);

and numCont is your own TextEditingController.

Here is the full code:

onInputChanged: (PhoneNumber number) {
        numCont.selection = TextSelection.collapsed(offset: numCont.text.length);
        },