搜索插件、代码仓与 APP 分类

phone_input 封面
插件详情v1.0.3

phone_input

该包旨在简化从用户那里获取电话号码的过程。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.6万
喜欢数
46
Pub 分数
160

安装插件

flutter pub add phone_input

插件介绍

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

phone_input 包通过提供直观的界面,支持自动检测国家代码、输入掩码和实时验证,简化了电话号码的收集。它支持 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台,确保在各种设备上提供一致的用户体验。该包可无缝集成到 Flutter 应用中,通过简单命令即可轻松设置,并提供可自定义的样式。它通过根据用户选择的国家引导用户进行正确的号码格式化,从而提高表单准确性。

适合场景

  • 需要国际电话号码的表单输入
  • 用户注册流程
  • 客户支持联系方式收集
  • 电子商务结账流程
  • 多区域应用本地化

主要特点

  • 自动国家代码检测
  • 实时输入掩码和格式化
  • 内置的电话号码验证
  • 对多平台的支持
  • 可自定义的 UI 组件
  • 与国家选择器的集成

适合对象

  • 需要电话号码输入的移动和网络应用程序
  • 寻求快速实现电话号码字段的开发人员
  • 面向全球受众的应用
  • 需要准确处理国际号码的表单
  • 强调用户在数据录入中体验的项目

使用建议

  • 需要互联网访问以进行完整的国家代码查找
  • 可能需要额外的样式以保持品牌一致性
  • 依赖于底层国家选择逻辑
  • 针对高级用例的有限自定义
  • 大数据集可能带来的性能影响

原始 README

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

phone_input

The Phone Input is a versatile package that provides cross-platform support for phone number input fields. This package is designed to simplify the process of capturing phone numbers from users.

It was based on the Phone Form Field package - https://pub.dev/packages/phone_form_field

Features

  • Cross-Platform Compatibility
  • Automated phone number formatting
  • Realm-Time phone number validation
  • Different methods for selecting countries with flexible customization
  • Integration with Customs Text Fields
  • Localization for different regions

Demo

Getting Started

Just create a PhoneInput widget and pass desired country selector that's it, you can already use it.


PhoneInput(countrySelectorNavigator: const CountrySelectorNavigator.dialog()),

Of course, you can pass any available params for customization of your Phone Input


PhoneInput(
  key: const Key('phone-field'),
  controller: null,     // Phone controller 
  initialValue: null,   // can't be supplied simultaneously with a controller
  shouldFormat: true    // default 
  defaultCountry: IsoCode.US, // default 
  decoration: InputDecoration(
  labelText: 'Phone',          // default to null
  border: OutlineInputBorder() // default to UnderlineInputBorder(),
  validator: PhoneValidator.validMobile(),   // default PhoneValidator.valid()
  isCountrySelectionEnabled: true, // default
  countrySelectorNavigator: CountrySelectorNavigator.bottomSheet(), // required 
  showFlagInInput: true,  // default
  flagShape: BoxShape.circle, // default
  showArrow: true,  // default
  flagSize: 16,           // default
  autofillHints: [AutofillHints.telephoneNumber], // default to null
  enabled: true,          // default
  autofocus: false,       // default
  onSaved: (PhoneNumber p) => print('saved $p'),   // default null
  onChanged: (PhoneNumber p) => print('changed $p'), // default null
// ... + other textfield params
),

Country selector

You can choose from many different selectors to select a country. All of them are flexibly customizable, and the default appearance selectors like dialog and bottom sheets depend on your app's theme settings.

Here is the list of the parameters available for all built-in country selectors:

  • countries - The list of countries that will be available in the list view widget.
  • favorites - The list of countries to be displayed at the top of the list.
  • addFavouriteSeparator - The boolean value determines whether to display divider between favorite and default countries.
  • showCountryCode - The boolean value determines whether to display the country dial code.
  • showCountryName - The boolean value determines whether to display the country name.
  • showCountryFlag - The boolean value determines whether to display the country flag.
  • noResultMessage - The String value that will display when a search result is empty.
  • countryCodeStyle - The text style of a country code.
  • countryNameStyle - The text style of a country name.
  • flagSize - Flag icon size.
  • flagShape - The BoxShape that determines if the displayed flags should be circular.
Built-in country selector
Name Description
Search Delegate Open a page for choosing the country
Dialog Open a dialog for choosing the country
Bottom Sheet Open a bottom sheet that expands to occupy all available space in both the horizontal and vertical axes.
Modal Bottom Sheet Open a modal bottom sheet that is expanded in the horizontal direction.
Draggable Bottom Sheet Open a modal bottom sheet that is expanded horizontally and can be dragged from a minimum to a maximum height based on the currently available space.
Dropdown Open a dropdown menu under text input with customizable height.
Extra parameters
  • CountrySelectorNavigator.searchDelegate

    No extra parameters.

  • CountrySelectorNavigator.dialog

    Extra parameters:

    • showSearchInput Whether to show the search input
    • searchInputDecoration The [InputDecoration] of the Search Input
    • searchInputTextStyle The [TextStyle] of the Search Input
    • defaultSearchInputIconColor The [Color] of the Search Icon in the Search Input
    • searchInputHeight The height of the search input field, if specified.
    • searchInputWidth The width of the search input field, if specified.
  • CountrySelectorNavigator.bottomSheet

    Extra parameters:

    • bottomSheetDragHandlerColor The [Color] of the divider at the top on the bottom sheet
    • and other extra params as in the dialog
  • CountrySelectorNavigator.modalBottomSheet

    Extra parameters:

    • height Provides the capability to define the height of the bottom sheet.
    • and other extra params as in the bottomSheet
  • CountrySelectorNavigator.draggableBottomSheet

    Extra parameters:

    • initialChildSize Defines the initial height of the bottom sheet.
    • minChildSize Defines the minimum height that the bottom sheet can occupy.
    • maxChildSize Defines the maximum height that the bottom sheet can occupy.
    • borderRadius Controls the rounded corner radius of the bottom sheet.
    • and other extra params as in the dialog
  • CountrySelectorNavigator.dropdown

    Since dropdown is implemented using overlay it is necessary to pass Layer Link.

    Extra parameters:

    • required layerLink The LayerLink for overlay positioning
    • borderRadius The dropdown's border-radius
    • listHeight The height of the dropdown list
    • backgroundColor The background color of the dropdown
    • and other extra params as in the dialog

Integration with Customs Text Fields

You can utilize any country selectors with either a customized or standard text field, allowing you to obtain an instance of the chosen country. To achieve this, you only need to invoke the requestCountrySelector() function on the desired selector while providing the necessary parameters.

For example:


TextField(
  onTap: () async {
    final country = await CountrySelectorNavigator.dialog().requestCountrySelector(context);
    print(country);
  },
),


Note that with bottomSheet selector, you need to additionally wrap the TextField in Builder.

Example:


Builder(
  builder: (context) {
    return TextField(
      onTap: () async {
      final country = await const CountrySelectorNavigator.bottomSheet().requestCountrySelector(context);
      print(country);
      },
    );
  },
),

Also, note what you need to do with with dropdown selector:

  1. Start by wrapping your TextField with CompositedTransformTarget.
  2. Provide a LayerLink to CompositedTransformTarget.
  3. Wrap Your TextField with a Builder.

For example:


CompositedTransformTarget(
  link: layerLink,
    child: Builder(
      builder: (context) {
        return TextField(
        onTap: () {
        final country = CountrySelectorNavigator.dropdown(
          backgroundColor: Colors.red,
          layerLink: layerLink,
          showSearchInput: true,
          ).requestCountrySelector(context);
        },
      );
    },
  ),
),

Validation

With Phone Input widget you can use a number of build-it and your own custom validators for phone numbers.

  • You can customize validate behavior through autovalidateMode parameter in Phone Input widget.
  • Each validator can be customized with an optional errorText property to change the displayed error message.
  • Most of the validators have an optional allowEmpty property that prevents the field from being flagged as valid if it is empty. This can be useful if you want to display a different error message for empty fields.
Built-in validators

Phone Input has the following built-in validators

Name Description
required This validator ensures that a phone number field is required and must be filled in.
valid This validator checks if the provided phone number is valid
valid country This validator checks if the provided phone number is valid within the specified country.
valid type This validator validates the phone number based on its type, such as mobile or fixed-line, ensuring it matches the specified type.
valid mobile number This validator verifies that the phone number provided is a valid mobile number
valid fixed line number This validator confirms the validity of the phone number as a fixed-line number
none Using this validator disables the default validation
Composing validators

You can also create your own custom validators by using the PhoneValidator.compose() method. This method takes a list of built-in or custom validators as input and combines the functionality of all of the input validators.

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


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

Internationalization

The following code demonstrates the integration of internationalization within your Flutter application using the MaterialApp widget:

    return MaterialApp(
      localizationsDelegates: [
      GlobalMaterialLocalizations.delegate,
      PhoneFieldLocalization.delegate
    ],
      supportedLocales: [
      const Locale('en', ''),
      const Locale('es', ''),
      const Locale('fr', ''),
      const Locale('uk', ''),
      // Add more locales as needed
    ],

A bunch of languages are built-in:

- 'ar',
- 'de',
- 'en',
- 'el'
- 'es',
- 'fr',
- 'hin',
- 'it',
- 'nl',
- 'pt',
- 'ru',
- 'uk',
- 'tr',
- 'zh',
- 'sv',

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目

暂无相关数据。