搜索分类、插件与代码仓

country_calling_code_kit 封面
插件详情v2.0.0

country_calling_code_kit

一个简洁的 Flutter 包,用于选择国家代码,包含国旗、名称和拨号代码。可自定义 UI,支持平台,使用友好。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
422
喜欢数
1
Pub 分数
160

安装插件

flutter pub add country_calling_code_kit

插件介绍

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

一个简洁的 Flutter 包,可让用户选择带有国旗、国家名称和拨号代码的国家代码。设计简单且可自定义,可在 Android、iOS、Web、macOS、Windows 和 Linux 上提供干净、平台适配的界面。非常适合需要国际电话号码输入的表单,只需少量设置即可确保无缝的用户体验。

适合场景

  • 国际电话号码输入
  • 用户入门引导,包含国家选择
  • 多区域应用本地化
  • 联系表单增强功能
  • 旅行或全球服务应用
  • 需要国家代码的身份验证流程

主要特点

  • 集成国家旗帜
  • 支持拨号代码和国家名称
  • 可自定义的 UI 组件
  • 跨平台兼容性
  • 与 Flutter 表单轻松集成
  • 响应式且可访问的设计

适合对象

  • 开发全球应用的开发者
  • 表单密集型应用
  • 面向国际用户的应用程序
  • 需要国家选择的移动和Web应用
  • 注重用户体验的开发人员,优先考虑直观的输入

使用建议

  • 需要手动处理所选国家的数据
  • 仅限国家代码和旗帜显示
  • 不支持手机号码的内置验证
  • 可能需要额外的样式以支持深色模式
  • 依赖外部标志资源以实现完整功能

原始 README

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

Country Calling Code Kit

A sleek, customizable Flutter package for picking country calling codes — complete with flags, country names, and dial codes. Present it as a dialog or a bottom sheet, tailor it to your app's design, and ship to every Flutter platform.

pub package license platforms

image1 image2 image3

Features

  • Dialog or bottom sheet — present the picker in whichever style fits your flow.
  • Automatic default country — detects the user's region from the device locale, no plugins or permissions required.
  • Built-in search — filter by country name, ISO code, or calling code.
  • Rich display — country flags, names, and dial codes in a clean, ready-to-use UI.
  • Highly customizable — text styles, colors, flag size, corner radii, dimensions, and shape.
  • Universal platform support — Android, iOS, Web, macOS, Linux, and Windows, with WebAssembly (WASM) compatibility.
  • Zero non-SDK dependencies — pure Dart and Flutter under the hood.

Installation

Add the package with the Flutter CLI:

flutter pub add country_calling_code_kit

Or add it to your pubspec.yaml manually:

dependencies:
  country_calling_code_kit: ^2.0.0

Then import it:

import 'package:country_calling_code_kit/country_calling_code_kit.dart';

Migrating to 2.0.0

Version 2.0.0 removes the native device_region dependency in favor of pure-Dart, locale-based detection. This makes the package WASM-compatible and available on all six platforms, but changes how the default country is resolved.

What changed: getDefaultCountry() now derives the region from the device/browser locale instead of the SIM card, and returns null when the locale has no region component (previously it fell back to the first country in the list).

What to do: provide your own fallback when the region can't be determined.

Future<void> loadDefaultCountry() async {
  // Before (1.x) — a non-null Country was effectively guaranteed:
  //   final Country country = (await getDefaultCountry())!;

  // After (2.0.0) — handle the nullable result explicitly:
  final Country country = await getDefaultCountry() ?? countries.first;
}

No other APIs changed. If you never called getDefaultCountry(), no migration is required.

Usage

Show a country picker dialog
Future<void> pickCountry(BuildContext context) async {
  final Country? country = await showCountryPickerDialog(context: context);

  if (country != null) {
    print('Name: ${country.name}'); // e.g. India
    print('Country code: ${country.countryCode}'); // e.g. in
    print('Calling code: ${country.callCode}'); // e.g. +91
  }
}
Show a country picker bottom sheet
Future<void> pickCountry(BuildContext context) async {
  final Country? country = await showCountryPickerModalSheet(context: context);

  if (country != null) {
    // Use the selected country.
  }
}
Detect the default country

Returns the [Country] matching the device's locale region, or null if it cannot be determined.

Future<void> loadDefaultCountry() async {
  final Country? defaultCountry = await getDefaultCountry();
  final Country country = defaultCountry ?? countries.first;
}
Look up a country by code
void findIndia() {
  final Country? india = getCountryByCountryCode(CountryCode.in_);
}
Access the full country list
void printCountries() {
  for (final country in countries) {
    print('${country.name} (${country.callCode})');
  }
}

Customization

Both showCountryPickerDialog and showCountryPickerModalSheet accept the following optional parameters:

Parameter Type Description
countryNameTextStyle TextStyle? Text style for country names.
countryCallCodeTextStyle TextStyle? Text style for calling codes.
imageSize Size? Size of the flag images (default 40 × 25).
splashColor Color? Splash color for list items.
hoverColor Color? Hover color for list items (web/desktop).
searchFilter List<Country> Function(String)? Custom search/filter logic.
preferredCountries List<CountryCode>? Countries pinned to the top of the list.
showCallCode bool Whether to show calling codes (default true).
search bool Whether to show the search bar (default true).
flagCornerRadius double? Corner radius for flag images (default 0).
itemBorderRadius double? Corner radius for each list item (default 10).
maxWidth double? Maximum width of the dialog/sheet.
maxHeight double? Maximum height of the dialog/sheet (default 80% / 85% of screen).
shape ShapeBorder? Shape border of the dialog/sheet (default rounded corners).

Example:

Future<void> pickCountry(BuildContext context) async {
  final country = await showCountryPickerModalSheet(
    context: context,
    preferredCountries: [CountryCode.us, CountryCode.gb, CountryCode.in_],
    showCallCode: true,
    flagCornerRadius: 4,
    itemBorderRadius: 12,
    maxWidth: 480,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.vertical(top: Radius.circular(28)),
    ),
  );
}
Using the widget directly

For full control over layout, embed CountryPicker yourself instead of using the dialog/sheet helpers:

Widget buildPicker() {
  return CountryPicker(
    onSelected: (country) {
      // Handle selection.
    },
    preferredCountries: [CountryCode.us, CountryCode.gb],
    showCallCode: true,
  );
}

Data model

Each result is a Country:

Field Type Description
name String Full country name.
flag String Asset path to the country's flag image.
countryCode CountryCode ISO 3166-1 alpha-2 code (as an enum value).
callCode String International calling code (e.g. +1).

CountryCode is an enum of ISO 3166-1 alpha-2 codes. Values that collide with Dart keywords use a trailing underscore (for example CountryCode.in_). Use CountryCode.fromString('in') to parse a code and code.toString() to get its string form.

Example

A complete, runnable example is available in the /example directory.

import 'package:country_calling_code_kit/country_calling_code_kit.dart';
import 'package:flutter/material.dart';

class CountryPickerDemo extends StatefulWidget {
  const CountryPickerDemo({super.key});

  @override
  State<CountryPickerDemo> createState() => _CountryPickerDemoState();
}

class _CountryPickerDemoState extends State<CountryPickerDemo> {
  Country? country;

  @override
  void initState() {
    super.initState();
    _initCountry();
  }

  Future<void> _initCountry() async {
    country = await getDefaultCountry() ?? countries.first;
    if (mounted) setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Country Picker Demo')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            if (country != null) ...[
              Image.asset(country!.flag, width: 100, height: 60),
              const SizedBox(height: 16),
              Text('Name: ${country!.name}'),
              Text('Code: ${country!.countryCode.toString().toUpperCase()}'),
              Text('Call Code: ${country!.callCode}'),
              const SizedBox(height: 24),
            ],
            ElevatedButton(
              onPressed: () async {
                final selected =
                await showCountryPickerDialog(context: context);
                if (selected != null) {
                  setState(() => country = selected);
                }
              },
              child: const Text('Select Country'),
            ),
          ],
        ),
      ),
    );
  }
}

Resources

Credits

Inspired by and with credit to country_calling_code_picker.

Contributing

Contributions are welcome!

If this package helps you, please consider giving it a ⭐️ on GitHub and a 👍 on pub.dev.

License

Released under the MIT License.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。