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.
Parse a phone number with region prefix and dashes.
Parse a phone number string without the region prefix. Region required. Country calling codes can be found here
Parsing a valid phone number results in a phone number object:
Validating
Validating a phone number requires both the phone number string and the region country code.
Formatting
Phone numbers can also be formatted for the UI to display the number.
PhoneNumber will not add the country prefix unless the phone number has the prefix
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).
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
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, ...)