phone_number 0.6.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 95

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.

LibraryVersion
libphonenumber8.12.1
PhoneNumberKit3.2.0

Usage #

Parsing #

TODO

Validating #

TODO

Formatting #

TODO

0.6.2 #

  • Upgrade to libphonenumber 8.12.1

0.6.1 #

  • Compatibility with Flutters Android Embedding V2
  • Activate E2E testing capabilities

0.6.0+2 #

  • Upgrade to libphonenumber 8.11.3
  • Upgrade to PhoneNumberKit 3.2.0

0.6.0+1 #

  • Thanks to @ened for helping fixing 2 iOS issues
  • Optimize factories for testing and normal usage
  • Add analytics_options w/ pedantic
  • Initial version of the README

0.6.0 #

  • Breaking Change: all the plugin's API methods are now instance methods

0.5.0 #

  • Add method to retrieve all supported regions & their country codes

0.4.1 #

  • Change channel name (add FQDN)
  • Reorganize the lib folder to Dart library standards
  • Update to libphonenumber 8.10.23 / PhoneNumberKitCore 3.1.0
  • Project cleanup

0.4.0 #

  • Add support for parsing a list of phone numbers

0.3.1 #

  • Add support for phone number's countryCode and nationalNumber

0.3.0 #

  • Add partial number formatting

0.2.0 #

  • Upgrade to libphonenumber 8.10.11 & PhoneNumberKit 2.6.0
  • Refactor to AndroidX
  • Remove Kotlin support and switched back to full Java implementation
  • Added a very simple test in the example app

0.1.1 #

  • Minimal implementation of the parse method. Example app still not functional.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:phone_number/phone_number.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  PhoneNumber _plugin = PhoneNumber();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      final parsed = await _plugin.parse("17449106505", region: "MX");

      platformVersion = """   
      
type: ${parsed['type']}
e164: ${parsed['e164']} 
international: ${parsed['international']}
national: ${parsed['national']}
country code: ${parsed['country_code']}
national number: ${parsed['national_number']}
      """;

      final formatted = await _plugin.format('+47234723432', 'BR');
      platformVersion += """
      
partially: ${formatted['formatted']}
      """;

      final parsedValues =
          await _plugin.parseList(["+48606723456", "+48774843312"]);

      parsedValues.forEach((number, parsed) {
        if (parsed != null) {
          platformVersion += """
          
type: ${parsed['type']}
e164: ${parsed['e164']}
international: ${parsed['international']}
national: ${parsed['national']}
country code: ${parsed['country_code']}
national number: ${parsed['national_number']}
          """;
        } else {
          platformVersion += """
          
number not recognized: $number
          """;
        }
      });
    } on PlatformException catch (e) {
      platformVersion = 'Failed: ${e.message}';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
          actions: <Widget>[
            Builder(builder: (context) {
              return IconButton(
                icon: Icon(Icons.list),
                tooltip: 'All Supported Regions',
                onPressed: () => _handleGetAll(context),
              );
            })
          ],
        ),
        body: Center(
            child: SingleChildScrollView(
          child: Text(
            _platformVersion,
            textAlign: TextAlign.left,
          ),
        )),
      ),
    );
  }

  Future<void> _handleGetAll(BuildContext context) async {
    final all = await _plugin.allSupportedRegions();

    print("all: $all");
    await showDialog(
      context: context,
      builder: (context) {
        final List<TableRow> rows = all.keys.map((regionCode) {
          return TableRow(children: [
            Text(regionCode),
            Text('${all[regionCode]}'),
          ]);
        }).toList();
        return AlertDialog(
          title: Text('All Supported Regions'),
          content: SingleChildScrollView(
            child: Table(children: rows),
          ),
        );
      },
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  phone_number: ^0.6.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:phone_number/phone_number.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
89
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
95
Learn more about scoring.

We analyzed this package on Apr 3, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
e2e ^0.4.0
flutter_driver
flutter_test
mockito ^4.1.0
pedantic ^1.0.0