libphonenumber_plugin 0.3.3 copy "libphonenumber_plugin: ^0.3.3" to clipboard
libphonenumber_plugin: ^0.3.3 copied to clipboard

Flutter package for Google libphonenumber, for Android and Web. Uses Flutter Federated Package for interoperability.

example/lib/main.dart

import 'dart:async';
import 'dart:developer';

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  final String _platformVersion = 'Unknown';

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

  Future<void> initPlatformState() async {
    try {
      String phoneNumber = '+212700000000';
      String regionCode = 'US';

      var formattedNumber =
          await PhoneNumberUtil.formatAsYouType(phoneNumber, regionCode);

      log('\n\n\n Output ==>> $formattedNumber \n\n\n');

      var regionInfo = await PhoneNumberUtil.getRegionInfo(phoneNumber, '');
      log('\n\n\n Output Valid ==>> $regionInfo \n\n\n');

      var countries = await PhoneNumberUtil.getAllCountries();

      var exampleNumber = await PhoneNumberUtil.getFormattedExampleNumber(
          regionCode, PhoneNumberType.MOBILE, PhoneNumberFormat.INTERNATIONAL);

      log('\n\n\n Output Countries ==>> $countries \n\n\n');
      log('\n\n\n Output Example Number ==>> $exampleNumber \n\n\n');

      phoneNumber = '+2348021234567';

      final normalizedNumber = await PhoneNumberUtil.normalizePhoneNumber(phoneNumber, 'NG', PhoneNumberFormat.NATIONAL);

      log('\n\n\n Output NormalizedNumber ===> $normalizedNumber \n\n\n');

      String digits = '';
      for (final number in phoneNumber.characters) {
        digits += number;
        final formattedNumber =
            await PhoneNumberUtil.formatAsYouType(digits, 'NG');

        log(' Output AsYouTypeFormatter ==>> $formattedNumber');
      }
    } on PlatformException catch (e) {
      log('\n\n\n PLATFORM EXCEPTION: \n\n\n $e \n\n\n');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
          actions: [
            IconButton(
              icon: const Icon(Icons.refresh),
              onPressed: () => initPlatformState(),
            )
          ],
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}
12
likes
150
pub points
94%
popularity

Publisher

unverified uploader

Flutter package for Google libphonenumber, for Android and Web. Uses Flutter Federated Package for interoperability.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_web_plugins, libphonenumber_platform_interface, libphonenumber_web

More

Packages that depend on libphonenumber_plugin