cedvdb / phone_form_field

Flutter phone number input
https://pub.dev/packages/phone_form_field
MIT License
70 stars 96 forks source link

phone_form_field

Flutter phone input integrated with flutter internationalization

Features

Demo

Demo available at https://cedvdb.github.io/phone_form_field/

Usage

PhoneFormField();

/// params
PhoneFormField(
  initialValue: PhoneNumber.parse('+33'), // or use the controller
  validator: PhoneValidator.compose(
      [PhoneValidator.required(), PhoneValidator.validMobile()]),
  countrySelectorNavigator: const CountrySelectorNavigator.page(),
  onChanged: (phoneNumber) => print('changed into $phoneNumber'),
  enabled: true,
  isCountrySelectionEnabled: true,
  isCountryButtonPersistent: true,
  countryButtonStyle: const CountryButtonStyle(
    showDialCode: true,
    showIsoCode: true,
    showFlag: true,
    flagSize: 16
  ),

  // + all parameters of TextField
  // + all parameters of FormField
  // ...
);

Validation

Built-in validators

Validators details

Composing validators

Validator can be a composed set of validators built-in or custom validator using PhoneValidator.compose, see example below.

Note that when composing validators, the sorting is important as the error message displayed is the first validator failing.

PhoneFormField(
  // ...
  validator: PhoneValidator.compose([
    // list of validators to use
    PhoneValidator.required(errorText: "You must enter a value"),
    PhoneValidator.validMobile(),
    // ..
  ]),
)

Country selector

Here are the list of the parameters available for all built-in country selector :

Name Default value Description
countries null Countries available in list view (all countries are listed when omitted)
favorites null List of country code ['FR','UK'] to display on top of the list
addSeparator true Whether to add a separator between favorite countries and others one. Useless if favorites parameter is null
showCountryCode true Whether to display the country dial code as listTile item subtitle
sortCountries false Whether the countries should appear in alphabetic order, if false the countries are displayed in the same order as countries property (Note that favorite countries are listed in supplied order whatever the value of this parameter)
noResultMessage null The message to be displayed in place of the list when search result is empty (a default localised message is used when omitted)

Built-in country selector

Custom Country Selector Navigator

You can use your own country selector by creating a class that implements CountrySelectorNavigator It has one required method show expected to return the selected country:

class CustomCountrySelectorNavigator implements CountrySelectorNavigator {
  Future<Country?> show(BuildContext context) {
    // ask user for a country and return related `Country` class
  }
}

// usage
PhoneFormField(
  // ...
  selectorNavigator: CustomCountrySelectorNavigator(),
  // ...
)

Internationalization

Dynamic localization

This package uses the flutter_country_selector package under the hood, which exports a method for dynamic localization CountrySelectorLocalization.of(context).countryName(isoCode).

Setup

Include the delegate

    return MaterialApp(
      localizationsDelegates: [
        DefaultMaterialLocalizations.delegate,
        ...PhoneFieldLocalization.delegates, 
      ], 
        supportedLocales: [ 
        const Locale('en', ''), 
        const Locale('es', ''), 
        const Locale('fr', ''), 
        const Locale('ru', ''), 
        const Locale('uz', ''), 
        const Locale('uk', ''), 
        // ... 
      ],

That's it.

Supported languages

If one of the language you target is not supported you can submit a pull request in flutter_country_selector and phone_form_field repositories.

Overwriting or adding custom flags

Some users have expressed their need to change some flags due to political reasons, or stylistic reasons. You might also wish to add your own flags. To do so refer to this issue: https://github.com/cedvdb/phone_form_field/issues/222