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

套件詳情v12.0.1

phone_form_field

與 Flutter 國際化整合的 Flutter 電話輸入

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
5.9萬
喜歡數
228
Pub 分數
160

安裝套件

flutter pub add phone_form_field

套件介紹

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

一個 Flutter 套件,提供具備內建國際化支援的電話號碼輸入欄位。它會根據使用者的地區設定自動格式化與驗證電話號碼,確保跨不同國家的正確輸入模式。整合於 Flutter 的本地化系統中,能動態適應語言與區域設定。此套件支援多個平台,包括 Android、iOS、Web、macOS、Windows 與 Linux,非常適合需要精確處理電話號碼的全球應用。

適用情境

  • 需要輸入手機號碼的國際應用
  • 表單中的多語言支援
  • 自動電話號碼格式化
  • 跨平台移動和網頁應用
  • 使用者友善的聯絡資訊蒐集
  • 動態國家代碼檢測

主要特色

  • 內建國際化
  • 即時電話號碼驗證
  • 按國家自動格式化
  • 支援所有主要平台
  • 與 Flutter i18n 的無縫整合
  • 可自訂輸入行為

適合對象

  • 全球應用
  • 表單密集型應用
  • 面向不同地區的應用
  • 使用 Flutter 本地化的開發者
  • 需要一致電話輸入使用者體驗的團隊

使用建議

  • 需要正確的本機化設定
  • 可能需要自定樣式以保持UI一致性
  • 依賴外部國家資料
  • 最初僅限支援的區域設定
  • 大型數據集可能帶來的效能影響
  • 需要在不同地區進行測試

原始 README

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

phone_form_field

Flutter phone input integrated with flutter internationalization

Features

  • Totally cross platform, this is a dart only package / dependencies
  • Internationalization: many languages supported
  • Semantics
  • Phone formatting localized by region
  • Phone number validation (built-in validators included for main use cases)
  • Support auto fill and copy paste
  • Uses dart phone_numbers_parser for parsing
  • Form field

Demo

Demo available at https://cedvdb.github.io/phone_form_field/

Usage

PhoneFormField();

/// params
PhoneFormField(
  initialValue: PhoneNumber.parse('+33'), // or use the controller
  validator: PhoneValidator.compose(
      [PhoneValidator.required(), PhoneValidator.validMobile()]),
  countrySelectorNavigator: const CountrySelectorNavigator.page(),
  onChanged: (phoneNumber) => print('changed into $phoneNumber'),
  enabled: true,
  isCountrySelectionEnabled: true,
  isCountryButtonPersistent: true,
  countryButtonStyle: const CountryButtonStyle(
    showDialCode: true,
    showIsoCode: true,
    showFlag: true,
    flagSize: 16
  ),

  // + all parameters of TextField
  // + all parameters of FormField
  // ...
);

Validation

Built-in validators
  • required : PhoneValidator.required
  • valid : PhoneValidator.valid (default value when no validator supplied)
  • valid mobile number : PhoneValidator.validMobile
  • valid fixed line number : PhoneValidator.validFixedLine
  • valid type : PhoneValidator.validType
  • valid country : PhoneValidator.validCountry
  • none : PhoneValidator.none (this can be used to disable default valid validator)
Validators details
  • Each validator has an optional errorText property to override built-in translated text
Composing validators

Validator can be a composed set of validators built-in or custom validator using PhoneValidator.compose, see example below.

Note that when composing validators, the sorting is important as the error message displayed is the first validator failing.

PhoneFormField(
  // ...
  validator: PhoneValidator.compose([
    // list of validators to use
    PhoneValidator.required(errorText: "You must enter a value"),
    PhoneValidator.validMobile(),
    // ..
  ]),
)

Country selector

Here are the list of the parameters available for all built-in country selector :

Name Default value Description
countries null Countries available in list view (all countries are listed when omitted)
favorites null List of country code ['FR','UK'] to display on top of the list
addSeparator true Whether to add a separator between favorite countries and others one. Useless if favorites parameter is null
showCountryCode true Whether to display the country dial code as listTile item subtitle
sortCountries false Whether the countries should appear in alphabetic order, if false the countries are displayed in the same order as countries property (Note that favorite countries are listed in supplied order whatever the value of this parameter)
noResultMessage null The message to be displayed in place of the list when search result is empty (a default localised message is used when omitted)
Built-in country selector
  • CountrySelectorNavigator.page Open a page to select the country. No extra parameters

  • CountrySelectorNavigator.dialog Open a dialog to select the country. No extra parameters

  • CountrySelectorNavigator.bottomSheet Open a bottom sheet expanding to all available space in both axis No extra parameters

  • CountrySelectorNavigator.modalBottomSheet Open a modal bottom sheet expanded horizontally Extra parameters:

    • height (double, default null) Allow to determine the height of the bottom sheet, will expand to all available height when omitted
  • CountrySelectorNavigator.draggableBottomSheet Open a modal bottom sheet expanded horizontally which may be dragged from a minimum to a maximum of current available height. Uses internally the DraggableScrollableSheet flutter widget Extra parameters:

    • initialChildSize (double, default: 0.5) factor of current available height used when opening
    • minChildSize (double, default: 0.Z5) : maximum factor of current available height
    • minChildSize (double, default: 0.Z5) : minimum factor of current available height
    • borderRadius (BorderRadiusGeometry, default: 16px circular radius on top left/right)
Custom Country Selector Navigator

You can use your own country selector by creating a class that implements CountrySelectorNavigator It has one required method show expected to return the selected country:

class CustomCountrySelectorNavigator implements CountrySelectorNavigator {
  Future<Country?> show(BuildContext context) {
    // ask user for a country and return related `Country` class
  }
}

// usage
PhoneFormField(
  // ...
  selectorNavigator: CustomCountrySelectorNavigator(),
  // ...
)

Internationalization

Dynamic localization

This package uses the flutter_country_selector package under the hood, which exports a method for dynamic localization CountrySelectorLocalization.of(context).countryName(isoCode).

Setup

Include the delegate

  return MaterialApp(
    localizationsDelegates: const [
      ...GlobalMaterialLocalizations.delegates,
      ...PhoneFieldLocalization.delegates, 
    ], 
      supportedLocales: [ 
      const Locale('en', ''), 
      const Locale('es', ''), 
      const Locale('fr', ''), 
      const Locale('ru', ''), 
      const Locale('uz', ''), 
      const Locale('uk', ''), 
      // ... 
    ],

That's it.

Supported languages
  • ar
  • ca
  • cs
  • de
  • el
  • en
  • es
  • fa
  • fr
  • he
  • hi
  • hu
  • it
  • ko
  • nb
  • nl
  • pt
  • ru
  • sk
  • sv
  • tr
  • uk
  • uz
  • vi
  • zh

If one of the language you target is not supported you can submit a pull request in flutter_country_selector and phone_form_field repositories.

Overwriting or adding custom flags

Some users have expressed their need to change some flags due to political reasons, or stylistic reasons. You might also wish to add your own flags. To do so refer to this issue: https://github.com/cedvdb/phone_form_field/issues/222

所屬分類

依賴

依賴清單 5 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。