搜索插件、代码仓与 APP 分类

country_calling_code_picker 封面
插件详情v2.0.1

country_calling_code_picker

灵活的国家选择器,用于获取国家代码和电话区号。

AndroidiOS
近 30 天下载
229
喜欢数
45
Pub 分数
150

安装插件

flutter pub add country_calling_code_picker

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个灵活且可自定义的 Flutter 国家选择器小部件,允许用户选择国家并获取其对应的国家代码和国际电话区号。专为无缝集成到需要输入国际电话号码的表单中而设计,支持 Android 和 iOS 平台。该包提供直观的界面,包含搜索功能和国旗显示,提升用户在选择国家时的体验。轻量级且易于实现,非常适合需要准确国际拨号信息的应用程序。

适合场景

  • 国际电话号码输入
  • 用户资料设置,包含国家选择
  • 多区域应用本地化
  • 带国家代码支持的联系表单
  • 需要电话验证的身份验证流程

主要特点

  • 可搜索的国家列表
  • 国家旗帜的显示
  • 自动检索区号
  • 可自定义的UI主题
  • 同时支持国家代码和区号输出

适合对象

  • 需要国际电话输入的移动应用
  • 用户引导流程
  • 全球电子商务平台
  • 消息或通话应用
  • 包含国际联系信息的表单

使用建议

  • 需要手动处理国家代码存储
  • 超出基础样式的有限自定义
  • 不支持手机号码的内置验证
  • 依赖外部数据源获取国家列表
  • 可能需要额外的逻辑来匹配时区或语言

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Country Calling Code Picker

pub package

A searchable country picker for Flutter, ready to use in a dialog, a bottom sheet or full screen. Gives you the country name, its ISO code, its international calling code and a bundled flag image for 250 countries and territories.

Pure Dart — no platform channels, no native build setup. It runs on Android, iOS, web, macOS, Windows and Linux.

Install

dependencies:
  country_calling_code_picker: ^3.0.0
import 'package:country_calling_code_picker/picker.dart';

Usage

Pre-select the country the device is set to
class _MyPageState extends State<MyPage> {
  Country? _selectedCountry;
  bool _didInit = false;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (!_didInit) {
      _didInit = true;
      _initCountry();
    }
  }

  Future<void> _initCountry() async {
    final country = await getDefaultCountry(context);
    if (!mounted) return;
    setState(() => _selectedCountry = country);
  }
}

getDefaultCountry resolves the device region from the platform locale. Pass fallbackCountryCode to choose what happens when the platform reports a region that is not in the list:

final country = await getDefaultCountry(context, fallbackCountryCode: 'US');
Bottom sheet
final country = await showCountryPickerSheet(context);
if (country != null) {
  setState(() => _selectedCountry = country);
}
Dialog
final country = await showCountryPickerDialog(context);
Full screen
class PickerPage extends StatelessWidget {
  const PickerPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Select country')),
      body: CountryPickerWidget(
        onSelected: (country) => Navigator.pop(context, country),
      ),
    );
  }
}
Just the data
// Every country, for your own custom picker.
final List<Country> countries = await getCountries(context);

// Or without a BuildContext, e.g. from a repository or a unit test.
final List<Country> countries = await getCountriesFromBundle();

// A single country by ISO code (case insensitive, null when unknown).
final Country? india = await getCountryByCountryCode(context, 'IN');

// The device region, e.g. 'IN'. Null when the platform reports none.
final String? code = getDeviceCountryCode(context);
Showing a flag

Flag images ship inside this package, so they need the package name:

Image.asset(country.flag, package: countryCodePackageName, width: 32);

Or skip the images entirely and use the emoji flag:

Text(country.flagEmoji); // 🇮🇳

CountryPickerWidget options

Parameter Default Description
onSelected null Called with the tapped Country.
itemTextStyle TextStyle(fontSize: 16) Text style of a list item.
searchInputStyle TextStyle(fontSize: 16) Text style of the search box.
searchInputDecoration null Replaces the search box decoration entirely.
searchHintText 'Search country name, code' Hint of the search box.
flagIconSize 32 Width of the flag image.
showSeparator false Draw a Divider between items.
focusSearchBox false Open the keyboard as soon as the picker shows.
showDeviceCountryFirst true Move the device's country to the top of the list.

Search matches the country name (accent insensitive, so aland finds Åland), the ISO code, and the calling code with or without its leading +.

Upgrading from 2.x

Version 3.0.0 requires Dart 3 / Flutter 3.24 or newer.

  • The flutter_sim_country_code dependency is gone. getDefaultCountry now reads the device region from the platform locale instead of the SIM card, which is why the package no longer needs any native code and works on web and desktop. On a device whose region setting and SIM disagree, the resolved country can differ from 2.x.
  • getCountryByCountryCode now really returns null for an unknown code. It used to throw a StateError. Its match is also case insensitive now.
  • The country matching the device is still moved to the top of the picker, but countries that merely share its calling code are no longer removed from the list — picking the US no longer made Canada disappear.
  • getDefaultCountry takes an optional fallbackCountryCode.
  • CountryPickerWidget.focusSearchBox is now actually forwarded by showCountryPickerSheet and showCountryPickerDialog.

License

MIT — see LICENSE.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。