flutter-form-builder-ecosystem / phone_number

Flutter plugin for parsing, formatting and validating international phone numbers.
https://pub.dev/packages/phone_number
BSD 3-Clause "New" or "Revised" License
81 stars 63 forks source link

Formatter: format numbers without country codes #105

Open andr2k opened 1 year ago

andr2k commented 1 year ago

Is there an existing issue for this?

Package/Plugin version

2.0.0

What you'd like to happen

Let's start with an example: we have a text field with a prefix, and the prefix contains the country code. The prefix is "+1", and the number is "123 456 78 90". the user enters the number without the country code, the country code is known.

Since we already can supply the formatter with the locale, it can lookup the country code. Consequently, an ability to format the number which does not include the code seems to be logical in such a scenario

Possibly, we could specify which parts of the phone number are included in the user-entered string. Something like that:

final _phoneController = PhoneNumberEditingController(
    PhoneNumberUtil(),
    behavior: PhoneInputBehavior.strict,
    locale: 'US',
    staticNumberParts: [
        PhoneParts.plusSign, // optional
        PhoneParts.countryCode(), // // required, can be "static" or "entered"; the code is obtained from the locale
    ],
    enteredNumberParts: [
        PhoneParts.areaCode(), // required, can be "static" or "entered"
    ],
);

When areaCode is known:

final _phoneController = PhoneNumberEditingController(
    PhoneNumberUtil(),
    behavior: PhoneInputBehavior.strict,
    locale: 'US',
    staticNumberParts: [
        PhoneParts.plusSign, // optional
        PhoneParts.countryCode(), // required, can be "static" or "entered"; the code is obtained from the locale
        PhoneParts.areaCode('123'), // required, can be "static" or "entered"
    ],

);

Any workarounds here?



### Alternatives you've considered

_No response_

### Aditional information

_No response_