Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

flutter_libphonenumber

a wrapper around libphonenumber with added functionality merged from the following libs:

uses the following native libraries:

platform library version
android libphonenumber 8.12.10
ios phonenumberkit 3.2.0

the main advantage to this lib is it lets you optionally format a phone number synchronously without making calls into libphonenumber with platform calls.

synchronous

format and parse

getting started

synchronous

first you need to call the init function. this will load all of the available regions available on the device from libphonenumber to build a formatting mask for each country using its example number from libphonenumber.

if you don’t run the init function then formatnumbersync will simply return the same thing passed into it without formatting anything as there won’t be any masks to utilize.

we use the same approach from flutter_multi_formatter for masking but instead of statically defining all of our country masks, we pull them on the fly from libphonenubmer so that the masks will be automatically maintained over time.

you can either do this during your app init before calling runapp, or with a futurebuilder as the example app demonstrates.

await flutterlibphonenumber().init();

formatting a number synchronously

synchronous

normally calls to libphonenumber’s format function are asynchronous, and you might not want
your ui rebuilding every time you need to format a phone number.

to get around this, we load a mask of every supported phone region’s example number from libphonenumber during the init() call. we can then use this mask to format an e164 phone number synchronously like this:

final rawnumber = '+14145556666';
final formattednumber = flutterlibphonenumber().formatphone(rawnumber); // +1 414-555-6666

countrymanager

synchronous

when you call init, this lib will store a list of the countries and phone metadata with the following class:

class countrywithphonecode {
  final string phonecode                            // '44',
  final string countrycode                          // 'gb',
  final string examplenumbermobilenational          // '07400 123456',
  final string examplenumberfixedlinenational       // '0121 234 5678',
  final string phonemaskmobilenational              // '00000 000000',
  final string phonemaskfixedlinenational           // '0000 000 0000',
  final string examplenumbermobileinternational     // '+44 7400 123456',
  final string examplenumberfixedlineinternational  // '+44 121 234 5678',
  final string phonemaskmobileinternational         // '+00 0000 000000',
  final string phonemaskfixedlineinternational      // '+00 000 000 0000',
  final string countryname                          // 'united kingdom';
}

synchronous

to access this list of countries you can get at it like this:

final countries = countrymanager().countries; // list<countrywithphonecode>
final defaultlocale = countrymanager().devicelocalecountrycode // us

api reference

synchronous

here is a reference of all of the available functions.

future<void> init({map<string, countrywithphonecode> overrides})

synchronous

must be called before we can format anything. this loads all available countries on the device and calls getallsupportedregions() to then cross-reference and combine everything and save a list<countrywithphonecode> of every available country with its phone code / mask.

optionally provide a map of overrides where the key is the country code (ex: gb or us) and the value is a countrywithphonecode object that should replace the data pulled from libphonenumber. this is useful if you want to customize the mask data for a given country.

future<map<string, countrywithphonecode>> getallsupportedregions()

returns all of the available regions on the device in a map with each key as the region code, and the value as a countrywithphonecode object containing all of the mobile and landline phone masks / example numbers for national / international format, phone code, region code, and country name.

example response:

{
  "uk": [countrywithphonecode()],
  "us": [countrywithphonecode()]
}

future<map<string, string>> format(string phone, string region)

formats a number using libphonenumber. under the hood this is using the asyoutype formatter. depending on your result, you might want to use formatnumbersync() to utilize the phone number masks.

will return the parsed / formatted number like this:

{
    formatted: "1 (414) 444-4444",
}

future<map<string, dynamic>> parse(string phone, {string region})

parses a number and if it is full and complete, returns some metadata associated with the number. the number must be a valid and compelte e164 formatted number to be considered valid.

will throw an error if the number isn’t valid.

example response:

{
    country_code: 49,
    e164: '+4930123123123',
    national: '030 123 123 123',
    type: 'mobile',
    international: '+49 30 123 123 123',
    national_number: '030123123123',
}

string formatnumbersync(string phone, {phonenumbertype phonenumbertype = phonenumbertype.mobile})

format a number synchronously using masks to format it. must have ran the init() function to pre-populate the mask data or else the original phone value will be returned.

optionally specify the phone number type to format it as (mobile vs fixedline). this is useful when a country has more than one phone number format and you want to format it to either fit the fixed line or mobile pattern.

example response:

"1 (414) 444-4444"

future<formatphoneresult> formatparsephonenumberasync(string phonenumber, countrywithphonecode country, {phonenumbertype = phonenumbertype.mobile})

asynchronously formats a phone number with libphonenumber. will return the formatted number and if it’s a valid/complete number, will return the e164 value as well in the e164 field. uses libphonenumber’s parse function to verify if it’s a valid number or not. this is useful if you want to format a number and also check if it’s valid, in one step.

optionally pass a phonenumbertype to format the number using either the mobile (default) or fixed line mask.

e164 will be null if the number is not valid.

class formatphoneresult {
  string formattednumber; // 1 (414) 444-4444
  string e164; // +14144444444
}

textinputformatter libphonenumbertextformatter(...)

the text formatter takes 3 optional arguments:

  • valuechanged<countrywithphonecode> oncountryselected is a callback that will be called when the formatter has automatically determined the country/region from the phone number. could be useful if you want to parse a phone number input in realtime and save the detected country from it once available.
  • string overrideskipcountrycode when this is supplied then we will format the number using the supplied country code and mask with the country code removed. this is useful if you have the country code being selected in another text box and just need to format the number without its country code in it.
  • futureor function(string val) onformatfinished optionally get a notification at this callback of the final formatted value once all formatting is completed. this is useful if you want to do something else that is triggered after formatting is done.
  • phonenumbertype phonenumbertype specify whether to format the phone number in the mobile format using the mask for mobile numbers, or the fixed line format. can either be phonenumbertype.mobile or phonenumbertype.fixedline. defaults to phonenumbertype.mobile.
  • phonenumberformat phonenumberformat specify to format using the national or international format. can either be phonenumberformat.international or phonenumberformat.national. defaults to phonenumberformat.international.

you should specify overrideskipcountrycode when you have a country picker somewhere else and just want your text field to format the number without the country code in it. this is useful if you have a dropdown to select the country code and a text field next to it to enter the number.

to use it, simply add libphonenumbertextformatter() to your textfield‘s inputformatters list:

textfield(inputformatters: [libphonenumbertextformatter(
    oncountryselected: (country) => print('oncountryselected: $country'),
    onformatfinished: (formattedval) => print('oncountryselected: $formattedval')
    overrideskipcountrycode: 'gb' // optionally override country to gb and return the number w/o +44. disabled auto-detection of country and forces using the mask of the provided country.
    phonenumbertype = phonenumbertype.fixedline // optionally format the number as something other than mobile
    phonenumberformat = phonenumberformat.international // optionally format the number in its international or national format pattern.
)])

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.