Coder Social home page Coder Social logo

Comments (21)

natintosh avatar natintosh commented on July 20, 2024 3

Sorry for the inconvenience @abdulwahid24 and the sudden breaking change, I updated the package to support a new initial value property that accepts a PhoneNumber object, so you basically use setState on your initial value object reference

PhoneNumber number =
        await PhoneNumber.getRegionInfoFromPhoneNumber(phoneNumber, 'US');

    setState(() {
      this.number = number;
    });

from intl_phone_number_input.

codekrafter avatar codekrafter commented on July 20, 2024

This is currently doable with a text controller but it would be nice to have this be dedicated field to avoid the overhead of a text controller

from intl_phone_number_input.

SpencerRiddering avatar SpencerRiddering commented on July 20, 2024

If I try to use the text controller method though, the phone number isn't formatted initially (no hyphens) by the form field. I don't see a way to utilize AsYouTypeFormatter for use when preparing my initial value list.

from intl_phone_number_input.

codekrafter avatar codekrafter commented on July 20, 2024

@SpencerRiddering I have the same issue

from intl_phone_number_input.

bscottsmith avatar bscottsmith commented on July 20, 2024

You will need to call the formatter directly and set the text controller. Something like:

PhoneNumberUtil.formatAsYouType(phoneNumber: "5555551212", isoCode: "US").then((formattedPhone) => _phoneInputController.text = formattedPhone);

You would either do that in your initState(), or if it's stateless you could use a FutureBuilder to do it.

from intl_phone_number_input.

Abhishek01039 avatar Abhishek01039 commented on July 20, 2024

hii @bscottsmith when I set the textController to InternationalPhoneNumberInput then it will give me an error like texteditingcontroller was used after being disposed
I am not using the dispose method anywhere then also I am getting this error
because I want to give an initial value to InternationalPhoneNumberInput so I need to give textEditingController

from intl_phone_number_input.

SpencerRiddering avatar SpencerRiddering commented on July 20, 2024

You will need to call the formatter directly and set the text controller. Something like:

PhoneNumberUtil.formatAsYouType(phoneNumber: "5555551212", isoCode: "US").then((formattedPhone) => _phoneInputController.text = formattedPhone);

You would either do that in your initState(), or if it's stateless you could use a FutureBuilder to do it.

For those following along at time, in order to do this you need to add package libphonenumber to your pubspec.yaml since PhoneNumberUtil is not exported by the intl-phone-number-input package.

from intl_phone_number_input.

bscottsmith avatar bscottsmith commented on July 20, 2024

Yes @Abhishek01039, you hit issue #26. Luckily it was just patched :)

from intl_phone_number_input.

natintosh avatar natintosh commented on July 20, 2024

For those following along at time, in order to do this you need to add package libphonenumber to your pubspec.yaml since PhoneNumberUtil is not exported by the intl-phone-number-input package.

You could call this static method

PhoneNumber.getParsableNumber

from intl_phone_number_input.

SpencerRiddering avatar SpencerRiddering commented on July 20, 2024

I think this issue could benefit from a concrete example. Say I have the following phone number which I've persisted to my database: +19162255887

When I go to display it using the InternationalPhoneNumberInput widget, I need to do two things:

  1. Get the dial code (+1 in this case), figure out what the corresponding country ISO code is ('US'), and then set the initialCountry2LetterCode parameter with this value.

  2. Format the remaining phone number (9162255887), and assign it to the initial value for the InternationalPhoneNumberInput widget. A formatted phone number in this case looks like this: 916-225-5887

Step one is easily done by PhoneNumber.getRegionInfoFromPhoneNumber(..)

Step two is not so easy. PhoneNumber.getParsableNumber('+19162255887', '+1') produces: 9162255887 and so it's not a solution for step two. What is needed is to call PhoneNumberUtil.formatAsYouType(..) with the phoneNumber from PhoneNumber.getParsableNumber() and the country ISO code from PhoneNumber.getRegionInfoFromPhoneNumber(..). Here is an example:

    String originalPhoneNumber = '+19162255887';
    PhoneNumber numberRegionInfo = await PhoneNumber.getRegionInfoFromPhoneNumber(originalPhoneNumber);
    String parsableNumber = PhoneNumber.getParsableNumber(originalPhoneNumber, numberRegionInfo.dialCode);
    String formattedNumber = await PhoneNumberUtil.formatAsYouType(phoneNumber: parsableNumber, isoCode: numberRegionInfo.isoCode);
    print(formattedNumber); // (916) 225-5887

This works in general, but has one issue: InternationalPhoneNumberInput formats dial code with a hyphen whereas PhoneNumberUtil.formatAsYouType(..) formats with parens.

916-225-5887 vs (916) 225-5887

Can someone explain why different formattings are produced? I seem to be using the same version of libphonenumber as intl-phone-number-input is (1.0.1)

from intl_phone_number_input.

natintosh avatar natintosh commented on July 20, 2024

Oh, I grab what you are trying to say. I actually thought I implemented something like that I'll work on it.

This works in general, but has one issue: InternationalPhoneNumberInput formats dial code with a hyphen whereas PhoneNumberUtil.formatAsYouType(..) formats with parens.

916-225-5887 vs (916) 225-5887

For the difference in the format, I do not know the cause of that I'll check it out but I wrote a custom TextInputFormatter that uses the PhoneNumberUtil.formatAsYouType(..) >> Here

from intl_phone_number_input.

natintosh avatar natintosh commented on July 20, 2024

Hi @SpencerRiddering, I think I finally know what's going on.

On the getRegionInfo for libphonenumber package,

  private void handleGetRegionInfo(MethodCall call, Result result) {
    final String phoneNumber = call.argument("phone_number");
    final String isoCode = call.argument("iso_code");

    try {
      Phonenumber.PhoneNumber p = phoneUtil.parse(phoneNumber, isoCode.toUpperCase());
      String regionCode = phoneUtil.getRegionCodeForNumber(p);
      String countryCode = String.valueOf(p.getCountryCode());
      String formattedNumber = phoneUtil.format(p, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);

      Map<String, String> resultMap = new HashMap<String, String>();
      resultMap.put("isoCode", regionCode);
      resultMap.put("regionCode", countryCode);
      resultMap.put("formattedPhoneNumber", formattedNumber);
      result.success(resultMap);
    } catch (NumberParseException e) {
      result.error("NumberParseException", e.getMessage(), null);
    }
  }

on this line

String formattedNumber = phoneUtil.format(p, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);

It uses the National format set by default.

While AsYouTypeFormatter doesn't have any specific format and its use both "-" and " " as its separator which is more like the International format form this link.

For the new changes you requested I am going to update the getParsableNumber to accepts two Strings phoneNumber and isoCode and return the formatted phoneNumber String

from intl_phone_number_input.

SpencerRiddering avatar SpencerRiddering commented on July 20, 2024

Thanks @natintosh I think that will work for me.

I didn't mean to distract from the issue originally reported by @codekrafter regarding the desire for an initial value parameter to the InternationalPhoneNumberInput widget. Example:

PhoneNumber initialPhoneNumber = PhoneNumber('+19162255887');
InternationalPhoneNumberInput(
    initialValue: initialPhoneNumber,
    ...
)

initialCountry2LetterCode wouldn't be required in this case since it's already contained within the PhoneNumber object.

from intl_phone_number_input.

natintosh avatar natintosh commented on July 20, 2024

Hi guys I just published an update thank you for your contributions 👍

from intl_phone_number_input.

abdulwahid24 avatar abdulwahid24 commented on July 20, 2024

@natintosh I highly appreciate your contribution, I upgraded with the latest package but I couldn't figure out how can we set initialValue because previously I was setting on the text controller.

PhoneNumber.getRegionInfoFromPhoneNumber(widget.needy.phone).then((phoneNumber){
      setState(() {
        phoneText.text = phoneNumber.parseNumber();
      });
    });

But now, I am facing issue as in edit form, When open the form the phone field with valid phone number shows invalid.
WhatsApp Image 2020-05-03 at 12 47 29 PM

from intl_phone_number_input.

abdulwahid24 avatar abdulwahid24 commented on July 20, 2024

Okay, Let me try out this.

from intl_phone_number_input.

abdulwahid24 avatar abdulwahid24 commented on July 20, 2024

Thank a ton @natintosh, It worked now. I was struggling for a few hours.

from intl_phone_number_input.

abdulwahid24 avatar abdulwahid24 commented on July 20, 2024

@natintosh I found the same issue on opening a new empty form, as it shouldn't validate initially without typing anything in the phone field.

WhatsApp Image 2020-05-03 at 1 22 56 PM

from intl_phone_number_input.

abdulwahid24 avatar abdulwahid24 commented on July 20, 2024

Also, the phone field gets clear after toggling a dialog popup.

from intl_phone_number_input.

abdulwahid24 avatar abdulwahid24 commented on July 20, 2024

I fixed the issue by putting a

textFieldController: phoneText,
ignoreBlank: true,

from intl_phone_number_input.

b02505048 avatar b02505048 commented on July 20, 2024

Sorry for the inconvenience @abdulwahid24 and the sudden breaking change, I updated the package to support a new initial value property that accepts a PhoneNumber object, so you basically use setState on your initial value object reference

PhoneNumber number =
        await PhoneNumber.getRegionInfoFromPhoneNumber(phoneNumber, 'US');

    setState(() {
      this.number = number;
    });

Hi @natintosh, I saw your example, the phoneNumber is a fixed number.
My question is how to retrieve the phoneNumber from user input and use it in PhoneNumber.getRegionInfoFromPhoneNumber(phoneNumber, 'US'), your help will be appreciated!!! Thank you!!

from intl_phone_number_input.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.