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

country_codes 封面
插件详情v3.3.0

country_codes

国家代码是一个辅助包,可根据特定本地化提供国家详细信息,例如拨号代码、ISO 3166代码(alpha-2、alpha-3)和国家名称。

AndroidiOSmacOS
近 30 天下载
3.4万
喜欢数
134
Pub 分数
150

安装插件

flutter pub add country_codes

插件介绍

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

国家代码是一个 Flutter/Dart 包,提供基于本地化的全面国家信息,包括拨号代码、ISO 3166 alpha-2 和 alpha-3 代码以及本地化国家名称。它通过在不同地区提供一致的国家详情访问,简化了国际数据的处理。该包轻量且专为表单、国家选择器和本地化工作流程设计。支持 Android、iOS 和 macOS 平台,非常适合需要准确国家元数据的跨平台应用。

适合场景

  • 国际电话号码输入
  • 表单中的国家选择
  • 国家名称的本地化
  • 使用 ISO 代码进行数据验证
  • 构建国家选择器小部件

主要特点

  • 支持多个国家标识符
  • 本地化国家名称
  • 拨号代码查找
  • ISO 3166 alpha-2 和 alpha-3 代码
  • 跨平台兼容性

适合对象

  • 需要国家数据的应用
  • 带国家选择的表单输入
  • 国际化功能
  • 拥有全球用户的移动应用
  • 开发国家选择器的开发者

使用建议

  • 需要手动处理区域设置回退
  • 无内置 UI 组件
  • 仅限静态国家数据
  • 不包含时区或货币信息
  • 依赖于正确的区域设置配置

原始 README

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

Country Codes

Country Codes Build Status Buy me a coffee

Country codes package provides an easy to way to access country codes based on a provided Locale.

Usage

There are two main ways to use this package.

1. Default by using device region

This will allow you to fetch the region of the underlying platform and display the data accordingly. Very handy if you use along with the DialCodeFormatter to provide integration with dial codes formatter, on phone TextFormFields in a Form, for example.

Assuming an en-US region based revice.

await CountryCodes.init(); // Optionally, you may provide a `Locale` to get countrie's localizadName

final Locale deviceLocale = CountryCodes.getDeviceLocale();
print(deviceLocale.languageCode); // Displays en
print(deviceLocale.countryCode); // Displays US

final CountryDetails details = CountryCodes.detailsForLocale();
print(details.alpha2Code); // Displays alpha2Code, for example US.
print(details.dialCode); // Displays the dial code, for example +1.
print(details.name); // Displays the extended name, for example United States.
print(details.localizedName); // Displays the extended name based on device's language (or other, if provided on init)
2. Use a custom Locale

This will use the provided Locale, which may not be related to the device's region but instead to the app supported languages.

For example, if your device is on US region but the app only supports PT, you'll get the following:

final CountryDetails details = CountryCodes.detailsForLocale(Localization.localeOf(context));

print(details.alpha2Code); // Displays alpha2Code, displays PT.
print(details.dialCode); // Displays the dial code for PT, +351.
print(details.name); // Displays the extended name, Portugal.

Formatters

DialCodeFormatter

Currently there's only available TextInputFormatter which allows you to dynamically set the dial code dynamically on a TextFormField.

TextFormField(
  keyboardType: TextInputType.phone,
  inputFormatters: [DialCodeFormatter()],
);

Example app

Example

Getting Started

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

所属分类

源码仓库

使用此插件的项目