fl_country_code_picker

style: very good analysisLicense: MIT

A Flutter package for showing a modal that contains country dial code. The user can search for the available codes and select right from the modal. Also, it has an automatic scrolling feature that points at current device's locale. Supports localizations!

📌 Examples

🔨 Installation

dependencies:
    fl_country_code_picker: ^0.0.4

⚙ Import

import  'package:fl_country_code_picker/fl_country_code_picker.dart';

đŸ•šī¸ Usage

Instantiate FlCountryCodePicker class to access the package's functionalities and properties. You can also pass some optional parameters to customize the picker's view.

final countryPicker = const  FlCountryCodePicker();
final countryPickerWithParams = const FlCountryCodePicker(
      favorites: _yourFavorites,
      favoritesIcon: _yourIcon,
      filteredCountries: _yourFilters,
      localize: true,
      searchBarDecoration: _yourInputDecoration,
      showDialCode: true,
      showFavoritesIcon: true,
      showSearchBar: true,
      title: _yourModalTitleWidget,
    );

Call the modal for country code picker.

    GestureDetector(
    onTap: () async {
        // Show the country code picker when tapped.
        final code = await countryPicker.showPicker(context: context);
        // Null check
        if (code != null) print(code);
    },
    child: Container(
        padding: const  EdgeInsets.symmetric(
        horizontal: 8.0, vertical: 4.0),
        margin: const  EdgeInsets.symmetric(horizontal: 8.0),
        decoration: const  BoxDecoration(
        color: Colors.blue,
        borderRadius: BorderRadius.all(Radius.circular(5.0))),
        child: Text('Show Picker', style: const  TextStyle(color: Colors.white)),
        ),
    ),

FlCountryCodePicker

FlCountryCodePicker class contains all of the functionalities of this package. This contains (optional) properties that can be supply to achieve customization at picker's view or appearance.

FieldsTypeDescription
favoritesList<String>?Favorite countries that can be shown at the top of the list. Should supply the 2 character ISO code of the country e.g. ['US', 'PH', 'AU']
favoritesIconIconCustom icon of favorite countries. Defaults to â¤ī¸.
filteredCountriesList<String>?Filters all of the CountryCodes available and only show the codes that are existing in this list. Should supply the 2 character ISO code of the country e.g. ['US', 'PH', 'AU']
localizeboolAn optional argument for localizing the country names based on device's current selected Language (country/region). Defaults to true.
searchBarDecorationInputDecoration?An optional argument for appearance customization of modal's search bar.
showDialCodeboolAn optional argument for showing dial code at country tiles. Defaults to true.
showFavoritesIconboolAn optional argument for showing favorites icon. Defaults to true.
showSearchBarboolAn optional argument for showing search bar. Defaults to true.
showDialCodeboolAn optional argument for showing dial code at country tiles. Defaults to true.
titleWidget?An optional argument for modal's title customization.

showPicker

showPicker method under the FlCountryCodePicker class that can be used to show the country code picker.

FieldsTypeDescription
contextBuildContextA handle to the location of a widget in the widget tree. Required.
isFullScreenboolShows the modal in full screen mode. Defaults to false.
pickerMinHeightdoublePicker modal constraints for minimum height. Defaults to 150.
pickerMaxHeightdoublePicker modal constraints for maximum height. Defaults to 500.
scrollToDeviceLocaleboolProperty to automatically scroll at device's locale within the picker. Defaults to false.
initialSelectedLocaleString?The 2 character ISO code of the country where the scrollController will automatically scroll to.

CountryCode

CountryCode model can be used to manipulate the selected country code by the user.

FieldsTypeDescription
nameStringThe name of the country
codeStringThe 2 character ISO code of the country
dialCodeStringThe country dial code. By convention, international telephone numbers are represented by prefixing the country code with a plus sign (+). e.g. +1 for US
flagImageWidgetWidget that can be used on retrieving the selected country flag's image.
flagUriStringUri of this CountryCode located at package's directory to supply at Image widget if you're going to get the raw flag image.
flagImagePackageStringPackage to supply at Image widget if you're going to get the raw flag image.
localizeStringConvenient getter for localized version of this country code.

❓ FAQ:

  • How to use country code's flag directory in Image widget?
Image.asset(
        fit: fit,
        width: width,
        countryCode.flagUri,
        alignment: alignment,
        package: countryCode.flagImagePackage,
);
  • How to change modal's title?

First, Create your title Widget.

	const Widget title = Padding(
		padding: EdgeInsets.all(16),
		child: Text(
		'My Title',
		style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
		),
	);

Then pass it to FlCountryCodePicker's title parameter.

countryPicker = const  FlCountryCodePicker(title: title);
  • How to enable package's supported localization?

First, import the package and add an alias to prevent other import errors.

import  'package:fl_country_code_picker/fl_country_code_picker.dart' as flc;

Add the following values at your app's MaterialApp

MaterialApp(
	title: 'Your App',
	// Supported locales at the moment.
	// Cannot find your locale? Please make a request.
	supportedLocales: flc.supportedLocales.map((e) => Locale(e)),
	localizationsDelegates: const [
		// Package's localization delegate.
		CountryLocalizations.delegate,
		GlobalWidgetsLocalizations.delegate,
		GlobalMaterialLocalizations.delegate,
		GlobalCupertinoLocalizations.delegate,
	],
	// ... some omitted values
);

🐞 Bugs/Requests

If you encounter any problems feel open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are also welcome.

📃 License

MIT License

Libraries

fl_country_code_picker