搜尋套件、儲存庫與 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.

所屬分類

原始碼儲存庫

使用此套件的專案