Git Product home page Git Product logo

leyvaje / phone_number Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flutter-form-builder-ecosystem/phone_number

0.0 0.0 0.0 255 KB

Flutter plugin for parsing, formatting and validating international phone numbers.

Home Page: https://pub.dev/packages/phone_number

License: BSD 3-Clause "New" or "Revised" License

Shell 1.34% Ruby 3.61% Objective-C 1.11% Java 13.09% Dart 69.23% Swift 11.62%

phone_number's Introduction

Phone Number

Phone Number is a Flutter plugin that allows you to parse, validate, format and other utilities for to international phone numbers.

Pub Version GitHub Workflow Status Codecov CodeFactor Grade Discord


Features

  • Parsing phone number
  • Validate phone number
  • Format phone number
  • Get region from phone number
  • Get region from device

Dependencies

The plugin uses the native libraries libphonenumber for Android and PhoneNumberKit pod for iOS.

Library Version
libphonenumber 8.12.52
PhoneNumberKit 3.4.4

Usage

Setup

No especific setup required: only install the dependency and use :)

Basic use

String springFieldUSASimple = '+14175555470';

// Parsing
PhoneNumber phoneNumber = await PhoneNumberUtil().parse(springFieldUSASimple);

// Validate
bool isValid = await plugin.validate(springFieldUSASimple);

// Format
RegionInfo region = RegionInfo('US', 1);
String formatted = await PhoneNumberUtil().format(springFieldUSASimple, region.code); // +1 (417) 555-5470

See pud.dev example tab or github code for more details

Specific use

Phone number with dashes

Parse a phone number with region prefix and dashes.

// Parsing
String springFieldUSA = '+1-417-555-5470';
PhoneNumber phoneNumber = await PhoneNumberUtil().parse(springFieldUSA);

// Validate
bool isValid = await PhoneNumberUtil().validate(springFieldUSA);

// Format
String formatted = await PhoneNumberUtil().format(springFieldUSASimpleNoRegion, region.code); // +1 (417) 555-5470

Phone number without region (national number)

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);

// Parsing
PhoneNumber phoneNumber = await PhoneNumberUtil().parse(springFieldUSASimpleNoRegion, region: region);

// Validate
bool isValid = await PhoneNumberUtil().validate(springFieldUSASimpleNoRegion, region: region.code);

// Format
String formatted = await PhoneNumberUtil().format(springFieldUSASimpleNoRegion, region.code); // (417) 555-5470

Parsing model result

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,
  regionCode: "US",
  nationalNumber: 4175555470,
  errorCode: null,
}

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: Phone number editing controller demo

Regions

Fetching regions (country code and prefixes).

List<RegionInfo> regions = await plugin.allSupportedRegions();
// [ RegionInfo { code: IM, prefix: 44 }, RegionInfo { code: LU, prefix: 352 }, ... ]

A parsed phone number will give region code as well.

String springFieldUSASimple = '+14175555470';
PhoneNumber phoneNumber = await PhoneNumberUtil().parse(springFieldUSASimple);
phoneNumber.regionCode; // US

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, etc.)

String code = await plugin.carrierRegionCode();

Support

Contribute

You have some ways to contribute to this packages

  • Beginner: Reporting bugs or request new features
  • Intermediate: Implement new features (from issues or not) and created pull requests
  • Advanced: Join the organization like a member and help coding, manage issues, dicuss new features and other things

See contribution guide for more details

Questions and answers

You can join to our Discord server

Donations

Donate or become a sponsor of Flutter Form Builder Ecosystem

Become a Sponsor

Roadmap

Ecosystem

Take a look to our awesome ecosystem and all packages in there

Thanks to

Contributors

Made with contrib.rocks.

phone_number's People

Contributors

clouddevil avatar deandreamatias avatar dertuca avatar iamsahilsonawane avatar maqoo avatar nashfive avatar nicbn avatar onatcipli avatar patuoynageek avatar thienvu18 avatar via-guy avatar victoruvarov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.