Comments (21)
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.
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.
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.
@SpencerRiddering I have the same issue
from intl_phone_number_input.
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.
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.
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.
Yes @Abhishek01039, you hit issue #26. Luckily it was just patched :)
from intl_phone_number_input.
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.
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:
-
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.
-
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.
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.
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.
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.
Hi guys I just published an update thank you for your contributions
from intl_phone_number_input.
@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.
from intl_phone_number_input.
Okay, Let me try out this.
from intl_phone_number_input.
Thank a ton @natintosh, It worked now. I was struggling for a few hours.
from intl_phone_number_input.
@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.
from intl_phone_number_input.
Also, the phone field gets clear after toggling a dialog popup.
from intl_phone_number_input.
I fixed the issue by putting a
textFieldController: phoneText,
ignoreBlank: true,
from intl_phone_number_input.
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)
- Invalid country code error HOT 10
- Curser issue while formating numbers HOT 7
- Number field can't be selected by integration test
- Problem: The number input field on IOS add also the country code HOT 24
- [Question] Confusion about the attribute "DialCode"
- make search request non case sensitive
- On iOS after the phone number is valed country dialcode is added to the number so it appears twice HOT 2
- Autofill does not update the flag
- The phone number no loading
- Enable or Disable selection of country from list HOT 1
- Hello no issue it's mistake sorry :) HOT 1
- Phone number on IOS does not work with country code HOT 6
- Error: Invalid value, Input field allowing special characters
- validator .. needs refactoring
- mobile number is incorrect HOT 2
- Get Phone Detail from contact list
- isoCode: 'MA' validators accept only 06 prefix !
- Allow Usage of ISO Codes Instead of Country Logos in the Leading of Input Field
- Version soloving failed
- Material 3 SubTitle Style is Deprecated . can you please change as per the Material 3 and Make a Release HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from intl_phone_number_input.