搜尋套件、儲存庫與 APP 分類

canopas_country_picker 封面
套件詳情v0.0.10

canopas_country_picker

一個簡單、可自訂的 Flutter 國家選擇器,用於選擇國家或撥號代碼並支援搜尋功能。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
427
喜歡數
18
Pub 分數
150

安裝套件

flutter pub add canopas_country_picker

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個簡單且可自訂的 Flutter 套件,用於選擇國家或其撥號代碼,具備內建搜尋功能。專為增強需要選擇國家的表單中使用者體驗而設計,支援 Android、iOS、Web、macOS、Windows 與 Linux 等多個平台。此選擇器輕量級、易於整合,並允許開發者根據應用需求自訂外觀與行為。

適用情境

  • 使用者註冊並選擇國家
  • 帶國家代碼的電話號碼輸入
  • 國際表單欄位
  • 旅行或寄送地址收集
  • 國家名稱的多語言支援

主要特色

  • 可搜尋的國家清單
  • 可自訂的 UI
  • 支援撥號代碼
  • 跨平台相容性
  • 輕量且快速
  • 與 Flutter 表單輕鬆整合

適合對象

  • 需要國家選擇的行動與網頁應用
  • 面向國際用戶的應用程式
  • 需要電話號碼輸入的表單
  • 尋求簡單國家選擇器的開發者
  • 依賴最少的專案

使用建議

  • 僅限預定義國家資料
  • 無即時國家更新
  • 自定義可能需要額外的樣式知識
  • 不包含地理定位功能
  • 可能需要手動更新以新增國家

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

A Simple, Customizable Flutter Country picker for picking a Country or Dialing code with Search functionality.

Preview

How to use

If you want to show the country picker bottom sheet, use:

CountryCode? code = await showCountryCodePickerSheet(context: context);

If you want to show the country picker dialog, use:

CountryCode? code = await showCountryCodePickerDialog(context: context);

If you want to show the country list as a UI component, use the CountryCodeSelector widget:

CountryCodeSelector(
   onCountryCodeTap: (CountryCode code) {
      /// Specify country code tap event
   },
    //Note: Cannot provide both onCountryCodeTap and codeBuilder, as onCountryCodeTap will not work if you have provided codeBuilder.
)

How to get a country code from the country alpha-2:,

CountryCode code = CountryCode.getCountryCodeByAlpha2(
    countryAlpha2Code:"IN", // its case-insensitive you can use both IN or in
);

You can use await Alpha2CountryCode,getCurrentCountryCode() to obtain the current alpha-2 code:

String countryAlpha2Code = await Alpha2CountryCode.getCurrentCountryCode();

CountryCode code = CountryCode.getCountryCodeByAlpha2(
  countryAlpha2Code: countryAlpha2Code,
);

Note: Using await Alpha2CountryCode.getCurrentCountryCode() will return the current country alpha-2 code based on the IP location from the 'http://ip-api.com/json/' API.


You can also get the current country alpha-2 code based on the user's device region, using PlatformDispatcher locale.

CountryCode code = CountryCode.getCountryCodeByAlpha2(
    countryAlpha2Code: WidgetsBinding.instance.platformDispatcher.locale.countryCode, 
    /// if you have context, use View.of(context).platformDispatcher.locale.countryCode
);

Note: WidgetsBinding and View.of(context) are provided by the Flutter SDK.


How to get a country code from the country dial code,

CountryCode code = CountryCode.getCountryCodeByDialCode(
    dialCode: "+91",
);

Note: Instead of using a dial code, which can be shared by multiple countries, we recommend using the alpha-2 code.


How to get a country codes from the country dial code,

List<CountryCode> code = CountryCode.getCountryCodesByDialCode(
    dialCode: "+47",
);

If you want to show the localized country name, use:

String? l10nCountryName = countryCode.localizedName("en");

Customizations

For customizations, we are providing the CustomizationBuilders class to customize each section.

customizationBuilders: CustomizationBuilders(
  codeBuilder: (CountryCode code) {
    // Return something to change the country list item UI.
    // Note: onCountryCodeTap will be overridden by codeBuilder in customizationBuilders.

   // If want to do some customization in default country code view, you can use
    return DefaultCountryCodeListItemView(
      onCountryCodeTap: () {},
      code: code,
      locale: 'US', // To show a localized country name,
    );
  },
  
  codeSeparatorBuilder: (BuildContext context, int index) {
    // Return something to add a separator between country codes.
    return const SizedBox(); // Default
  },
  
  countryListBuilder: (List<CountryCode> codes, ScrollController? controller) {
    // Return something to customize the country list
  },
  
  textFieldBuilder: (void Function(String)? filter) {
    // Return the search text field widget
    //
    // If you want to do some customization in the default text field, you can use
    return DefaultCountryCodeFilterTextField(
      filter: filter,
    );

    // Use filter(searchText); to update the country code list.
  },
),

Bugs and Feedback

For bugs, feedback, questions and discussions please use the Github Issues.

Credits

Canopas Country Picker is owned and maintained by the Canopas team. You can follow us on Twitter at @canopassoftware for project updates and releases.

We regularly upload blogs on new topics, which you can read here.

所屬分類

原始碼儲存庫

使用此套件的專案