📜  flutter intl_phone_number - Dart (1)

📅  最后修改于: 2023-12-03 15:00:47.836000             🧑  作者: Mango

flutter intl_phone_number - Dart

intl_phone_number is a Dart package that provides phone number parsing, validation and formatting features. It can be used to easily validate and format phone numbers according to their international standards. This package is particularly useful for applications that require user input of phone numbers, such as e-commerce apps, banking apps, and social media apps.

Features

The main features of intl_phone_number package include:

  • Phone number validation
  • Phone number parsing
  • Phone number formatting
  • Support for multiple countries and their phone number formats
  • Easy to use API with a clear documentation
Installation

To use intl_phone_number package, add the following dependency to your pubspec.yaml file:

dependencies:
  intl_phone_number: ^0.6.1

Then, run flutter pub get to install the package.

Usage
Importing the package

First, import the intl_phone_number package in your Dart code:

import 'package:intl_phone_number/intl_phone_number.dart';
Validating a phone number

To validate a phone number, create an instance of the PhoneNumberUtil class and call its isValidNumber method with the phone number and the ISO 3166-1 alpha-2 country code. The method returns true if the phone number is valid for the given country or false otherwise.

final phoneNumber = '+1 555-555-5555';
final phoneNumberUtil = PhoneNumberUtil();
final isValid = phoneNumberUtil.isValidNumber(
  phoneNumber: phoneNumber,
  isoCode: 'US',
);
print('Is $phoneNumber a valid US phone number? $isValid');
Parsing a phone number

To parse a phone number, create an instance of the PhoneNumberUtil class and call its parse method with the phone number and the ISO 3166-1 alpha-2 country code. The method returns a PhoneNumber object that contains the parsed phone number components.

final phoneNumber = '+1 555-555-5555';
final phoneNumberUtil = PhoneNumberUtil();
final parsedNumber = phoneNumberUtil.parse(
  phoneNumber: phoneNumber,
  isoCode: 'US',
);
print('Parsed phone number: $parsedNumber');
Formatting a phone number

To format a phone number, create an instance of the PhoneNumberUtil class and call its format method with the PhoneNumber object and the PhoneNumberFormat enum value that specifies the desired format. The method returns the formatted phone number as a string.

final phoneNumber = '+1 555-555-5555';
final phoneNumberUtil = PhoneNumberUtil();
final parsedNumber = phoneNumberUtil.parse(
  phoneNumber: phoneNumber,
  isoCode: 'US',
);
final formattedNumber = phoneNumberUtil.format(
  phoneNumber: parsedNumber,
  format: PhoneNumberFormat.NATIONAL,
);
print('Formatted phone number: $formattedNumber');
Conclusion

The intl_phone_number package is a powerful tool for working with phone numbers in Dart. Its features for validating, parsing, and formatting phone numbers can be easily integrated into any Flutter application that requires phone number input from its users.