一个 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();
/// paramsPhoneFormField(
initialValue: PhoneNumber.parse('+33'), // or use the controller
validator: PhoneValidator.compose(
[PhoneValidator.required(), PhoneValidator.validMobile()]),
countrySelectorNavigator: constCountrySelectorNavigator.page(),
onChanged: (phoneNumber) => print('changed into $phoneNumber'),
enabled: true,
isCountrySelectionEnabled: true,
isCountryButtonPersistent: true,
countryButtonStyle: constCountryButtonStyle(
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 usePhoneValidator.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:
classCustomCountrySelectorNavigatorimplementsCountrySelectorNavigator{
Future<Country?> show(BuildContext context) {
// ask user for a country and return related `Country` class
}
}
// usagePhoneFormField(
// ...
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).
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