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

country_calling_code_picker 封面
套件詳情v2.0.1

country_calling_code_picker

靈活的國家選擇器,用於取得國家代碼和電話區號。

AndroidiOS
近 30 天下載
229
喜歡數
45
Pub 分數
150

安裝套件

flutter pub add country_calling_code_picker

套件介紹

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

一個靈活且可自訂的 Flutter 國家選擇器小組件,允許使用者選擇國家並取得對應的國家代碼與國際電話區號。專為無縫整合至需要輸入國際電話號碼的表單中而設計,支援 Android 與 iOS 平台。該套件提供直覺的介面,具備搜尋功能與國旗顯示,提升使用者在選擇國家時的體驗。輕量級且容易實作,非常適合需要準確國際撥號資訊的應用程式。

適用情境

  • 國際電話號碼輸入
  • 使用者資料設定,包含國家選擇
  • 多區域應用本地化
  • 帶國家代碼支援的聯絡表單
  • 需要電話驗證的驗證流程

主要特色

  • 可搜尋的國家列表
  • 國家旗幟的顯示
  • 自動檢索區號
  • 可自定義的UI主題
  • 同時支援國家代碼和撥號代碼輸出

適合對象

  • 需要國際電話輸入的行動應用程式
  • 使用者引導流程
  • 全球電商平台
  • 訊息或通話應用
  • 包含國際聯絡資訊的表單

使用建議

  • 需要手動處理國家代碼儲存
  • 超出基本樣式之外的有限自訂
  • 不支援電話號碼的內建驗證
  • 依賴外部資料來源取得國家清單
  • 可能需要額外的邏輯來匹配時區或語言

原始 README

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

Country Calling Code Picker

pub package

A searchable country picker for Flutter, ready to use in a dialog, a bottom sheet or full screen. Gives you the country name, its ISO code, its international calling code and a bundled flag image for 250 countries and territories.

Pure Dart — no platform channels, no native build setup. It runs on Android, iOS, web, macOS, Windows and Linux.

Install

dependencies:
  country_calling_code_picker: ^3.0.0
import 'package:country_calling_code_picker/picker.dart';

Usage

Pre-select the country the device is set to
class _MyPageState extends State<MyPage> {
  Country? _selectedCountry;
  bool _didInit = false;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (!_didInit) {
      _didInit = true;
      _initCountry();
    }
  }

  Future<void> _initCountry() async {
    final country = await getDefaultCountry(context);
    if (!mounted) return;
    setState(() => _selectedCountry = country);
  }
}

getDefaultCountry resolves the device region from the platform locale. Pass fallbackCountryCode to choose what happens when the platform reports a region that is not in the list:

final country = await getDefaultCountry(context, fallbackCountryCode: 'US');
Bottom sheet
final country = await showCountryPickerSheet(context);
if (country != null) {
  setState(() => _selectedCountry = country);
}
Dialog
final country = await showCountryPickerDialog(context);
Full screen
class PickerPage extends StatelessWidget {
  const PickerPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Select country')),
      body: CountryPickerWidget(
        onSelected: (country) => Navigator.pop(context, country),
      ),
    );
  }
}
Just the data
// Every country, for your own custom picker.
final List<Country> countries = await getCountries(context);

// Or without a BuildContext, e.g. from a repository or a unit test.
final List<Country> countries = await getCountriesFromBundle();

// A single country by ISO code (case insensitive, null when unknown).
final Country? india = await getCountryByCountryCode(context, 'IN');

// The device region, e.g. 'IN'. Null when the platform reports none.
final String? code = getDeviceCountryCode(context);
Showing a flag

Flag images ship inside this package, so they need the package name:

Image.asset(country.flag, package: countryCodePackageName, width: 32);

Or skip the images entirely and use the emoji flag:

Text(country.flagEmoji); // 🇮🇳

CountryPickerWidget options

Parameter Default Description
onSelected null Called with the tapped Country.
itemTextStyle TextStyle(fontSize: 16) Text style of a list item.
searchInputStyle TextStyle(fontSize: 16) Text style of the search box.
searchInputDecoration null Replaces the search box decoration entirely.
searchHintText 'Search country name, code' Hint of the search box.
flagIconSize 32 Width of the flag image.
showSeparator false Draw a Divider between items.
focusSearchBox false Open the keyboard as soon as the picker shows.
showDeviceCountryFirst true Move the device's country to the top of the list.

Search matches the country name (accent insensitive, so aland finds Åland), the ISO code, and the calling code with or without its leading +.

Upgrading from 2.x

Version 3.0.0 requires Dart 3 / Flutter 3.24 or newer.

  • The flutter_sim_country_code dependency is gone. getDefaultCountry now reads the device region from the platform locale instead of the SIM card, which is why the package no longer needs any native code and works on web and desktop. On a device whose region setting and SIM disagree, the resolved country can differ from 2.x.
  • getCountryByCountryCode now really returns null for an unknown code. It used to throw a StateError. Its match is also case insensitive now.
  • The country matching the device is still moved to the top of the picker, but countries that merely share its calling code are no longer removed from the list — picking the US no longer made Canada disappear.
  • getDefaultCountry takes an optional fallbackCountryCode.
  • CountryPickerWidget.focusSearchBox is now actually forwarded by showCountryPickerSheet and showCountryPickerDialog.

License

MIT — see LICENSE.

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。