phone number for flutter
phonenumber is a flutter plugin that allows you to parse, validate and format international phone numbers.
the plugin uses the native libraries libphonenumber for android and phonenumberkit pod for ios.
library | version |
---|---|
libphonenumber | 8.12.32 |
phonenumberkit | 3.3.3 |
usage
parsing
parse a phone number with region prefix.
string springfieldusasimple = '+14175555470';
phonenumber phonenumber = await phonenumberutil().parse(springfieldusasimple);
parse a phone number with region prefix and dashes.
string springfieldusa = '+1-417-555-5470';
phonenumber phonenumber = await phonenumberutil().parse(springfieldusa);
parse a phone number string without the region prefix. region required. country calling codes can be found here
string springfieldusasimplenoregion = '4175555470';
regioninfo region = regioninfo('us', 1);
phonenumber phonenumber = await phonenumberutil().parse(springfieldusasimplenoregion, region: region);
parsing a valid phone number results in a phone number object:
phonenumber{
e164: +14175555470,
type: phonenumbertype.fixed_line_or_mobile,
international: +1 417-555-5470,
national: (417) 555-5470,
countrycode: 1,
nationalnumber: 4175555470,
errorcode: null,
}
validating
validating a phone number requires both the phone number string and the region country code.
phonenumberutil plugin = phonenumberutil();
string springfieldusasimplenoregion = '4175555470';
regioninfo region = regioninfo('us', 1);
bool isvalid = await plugin.validate(springfieldusasimplenoregion, region.code);
string springfieldusasimple = '+14175555470';
bool isvalid = await plugin.validate(springfieldusasimple, region.code);
string springfieldusa = '+1-417-555-5470';
bool isvalid = await plugin.validate(springfieldusa, region.code);
formatting
phone numbers can also be formatted for the ui to display the number.
string springfieldusasimplenoregion = '4175555470';
regioninfo region = regioninfo('us', 1);
string formatted = await phonenumberutil().format(springfieldusasimplenoregion, region.code); // (417) 555-5470
phonenumber will not add the country prefix unless the phone number has the prefix
string springfieldusasimplenoregion = '+14175555470';
regioninfo region = regioninfo('us', 1);
string formatted = await phonenumberutil().format(springfieldusasimplenoregion, region.code); // +1 (417) 555-5470
as-you-type formatting
attach the provided phonenumbereditingcontroller
to a textfield to format its text as the user type.
there are 3 formatting behavior:
phoneinputbehavior.strict
: always format, do not accept non dialable chars.phoneinputbehavior.cancellable
: stop formatting when a separator is removed, do not accept non dialable chars.phoneinputbehavior.lenient
(default): stop formatting when either a non dialable char is inserted or a separator is removed.
example video: https://www.youtube.com/watch?v=rllgvxci-2y.
see example/lib/autoformat_page.dart
for a detailed implementation.
regions
fetching regions (country code and prefixes).
list<regioninfo> regions = await plugin.allsupportedregions();
// [ regioninfo { code: im, prefix: 44 }, regioninfo { code: lu, prefix: 352 }, ... ]
if you want to display all the flags alongside the regions in your ui region picker, consider having a json file instead of using this function. example json file
const list<map<string, dynamic>> countries = [
{"name":"afghanistan","flag":"����","code":"af","dial_code":"+93"},
{"name":"åland islands","flag":"����","code":"ax","dial_code":"+358"},
...
]
device region code
it is possible to fetch the region code from the device. this will give you the two letter country code. (e.g. us, uk, …)
string code = await plugin.carrierregioncode();
Comments are closed.