搜索插件、代码仓与 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.

所属分类

源码仓库

使用此插件的项目