Flutter国家代码选择器插件fl_country_code_picker_nic的使用
Flutter国家代码选择器插件fl_country_code_picker_nic的使用
fl_country_code_picker
该插件是一个用于显示包含国家拨号代码的弹窗的Flutter包。用户可以搜索可用的代码并在弹窗中直接选择。它还具有自动滚动功能,可以根据当前设备的语言定位。支持本地化!
📌 示例
🔨 安装
在 pubspec.yaml
文件中添加依赖:
dependencies:
fl_country_code_picker_nic: ^0.0.4
⚙ 导入
在你的 Dart 文件中导入插件:
import 'package:fl_country_code_picker_nic/fl_country_code_picker_nic.dart';
🕹️ 使用
首先,实例化 FlCountryCodePicker
类以访问插件的功能和属性。你还可以传递一些可选参数来自定义选择器的视图。
final countryPicker = const FlCountryCodePicker();
final countryPickerWithParams = const FlCountryCodePicker(
favorites: _yourFavorites,
favoritesIcon: _yourIcon,
filteredCountries: _yourFilters,
localize: true,
searchBarDecoration: _yourInputDecoration,
showDialCode: true,
showFavoritesIcon: true,
showSearchBar: true,
title: _yourModalTitleWidget,
);
调用模态框来显示国家代码选择器:
GestureDetector(
onTap: () async {
// 当点击时显示国家代码选择器
final code = await countryPicker.showPicker(context: context);
// 非空检查
if (code != null) print(code);
},
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
margin: const EdgeInsets.symmetric(horizontal: 8.0),
decoration: const BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(5.0)),
),
child: Text('显示选择器', style: const TextStyle(color: Colors.white)),
),
),
FlCountryCodePicker
FlCountryCodePicker
类包含了此包的所有功能。它包含了一些可选属性,可以通过这些属性来自定义选择器的视图或外观。
字段 | 类型 | 描述 |
---|---|---|
favorites | List<String>? | 可以显示在列表顶部的收藏国家。应提供国家的2字符ISO代码,如 ['US', 'PH', 'AU'] 。 |
favoritesIcon | Icon | 自定义收藏国家的图标,默认为 ❤️。 |
filteredCountries | List<String>? | 过滤所有可用的 [CountryCode] ,仅显示此列表中存在的代码。应提供国家的2字符ISO代码,如 ['US', 'PH', 'AU'] 。 |
localize | bool | 可选参数,基于设备当前选定的语言(国家/地区)进行本地化。默认为 true 。 |
searchBarDecoration | InputDecoration? | 模态框搜索栏外观自定义的可选参数。 |
showDialCode | bool | 可选参数,用于在国家方块中显示拨号代码。默认为 true 。 |
showFavoritesIcon | bool | 可选参数,用于显示收藏图标。默认为 true 。 |
showSearchBar | bool | 可选参数,用于显示搜索栏。默认为 true 。 |
title | Widget? | 可选参数,用于自定义模态框标题。 |
showPicker
showPicker
方法可以在 FlCountryCodePicker
类下使用,以显示国家代码选择器。
字段 | 类型 | 描述 |
---|---|---|
context | BuildContext | 小部件树中位置的句柄。必需。 |
isFullScreen | bool | 是否以全屏模式显示模态框。默认为 false 。 |
pickerMinHeight | double | 选择器模态框最小高度约束。默认为 150 。 |
pickerMaxHeight | double | 选择器模态框最大高度约束。默认为 500 。 |
scrollToDeviceLocale | bool | 属性用于在选择器内自动滚动到设备的语言。默认为 false 。 |
initialSelectedLocale | String? | 自动滚动到指定国家的2字符ISO代码。 |
CountryCode
CountryCode
模型可用于处理用户选择的国家代码。
字段 | 类型 | 描述 |
---|---|---|
name | String | 国家名称。 |
code | String | 国家的2字符ISO代码。 |
dialCode | String | 国家拨号代码。按惯例,国际电话号码表示法会在国家代码前加上一个加号(+)。例如,+1 代表美国。 |
flagImage | Widget | 可用于检索所选国家标志图像的小部件。 |
flagUri | String | 包目录中的 CountryCode URI,如果要获取原始标志图像,则应在 Image 小部件中提供。 |
flagImagePackage | String | 如果要获取原始标志图像,则应在 Image 小部件中提供的包。 |
localize | String | 此国家代码的本地化版本的便捷获取器。 |
❓ FAQ
- 如何在
Image
小部件中使用国家代码的标志目录?
Image.asset(
fit: fit,
width: width,
countryCode.flagUri,
alignment: alignment,
package: countryCode.flagImagePackage,
);
- 如何更改模态框的标题?
首先,创建你的标题 Widget
:
const Widget title = Padding(
padding: EdgeInsets.all(16),
child: Text(
'我的标题',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
);
然后将其传递给 FlCountryCodePicker
的 title
参数:
countryPicker = const FlCountryCodePicker(title: title);
- 如何启用包的支持本地化?
首先,导入包并添加别名以防止其他导入错误:
import 'package:fl_country_code_picker_nic/fl_country_code_picker_nic.dart' as flc;
在你的应用的 MaterialApp
中添加以下值:
MaterialApp(
title: '你的应用',
// 目前支持的语言。
// 找不到你的语言?请提出请求。
supportedLocales: flc.supportedLocales.map((e) => Locale(e)),
localizationsDelegates: const [
// 包的本地化代理。
flc.CountryLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
// ... 省略的值
);
更多关于Flutter国家代码选择器插件fl_country_code_picker_nic的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter国家代码选择器插件fl_country_code_picker_nic的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter应用中使用fl_country_code_picker_nic
插件的示例代码。这个插件用于显示国家代码选择器,用户可以选择一个国家,然后获取该国家的代码和拨号代码。
首先,确保你已经在pubspec.yaml
文件中添加了依赖项:
dependencies:
flutter:
sdk: flutter
fl_country_code_picker_nic: ^最新版本号 # 请替换为最新版本号
然后,运行flutter pub get
来获取依赖项。
接下来,在你的Flutter应用中,你可以按照以下方式使用这个插件:
import 'package:flutter/material.dart';
import 'package:fl_country_code_picker_nic/fl_country_code_picker_nic.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String? selectedCountryCode;
String? selectedDialCode;
String? selectedCountryName;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('国家代码选择器示例'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CountryCodePickerNic(
onChanged: (CountryCode countryCode) {
setState(() {
selectedCountryCode = countryCode.code;
selectedDialCode = countryCode.dialCode;
selectedCountryName = countryCode.name;
});
},
favorite: ['+1', '91', '+86'], // 可选:设置常用国家代码
initialSelection: 'US', // 可选:设置初始选择国家
selectorType: MobileCountryCodePickerSelectorType.dialog, // 或者 MobileCountryCodePickerSelectorType.dropdown
showFlag: true,
showCurrency: false,
showOnlyCountryWhenClosed: false,
),
SizedBox(height: 20),
Text('选择的国家代码: $selectedCountryCode'),
Text('选择的拨号代码: $selectedDialCode'),
Text('选择的国家名称: $selectedCountryName'),
],
),
),
);
}
}
在这个示例中:
CountryCodePickerNic
小部件用于显示国家代码选择器。onChanged
回调在用户选择一个国家时被调用,并返回所选国家的CountryCode
对象。favorite
参数用于设置常用国家代码列表。initialSelection
参数用于设置初始选择的国家代码(这里使用的是国家代码缩写,例如'US'
表示美国)。selectorType
参数用于设置选择器类型,可以是对话框(MobileCountryCodePickerSelectorType.dialog
)或下拉菜单(MobileCountryCodePickerSelectorType.dropdown
)。showFlag
参数控制是否显示国旗图标。showCurrency
参数控制是否显示货币符号(在这个例子中设置为false
)。showOnlyCountryWhenClosed
参数控制当选择器关闭时是否只显示国家名称。
这个示例展示了如何使用fl_country_code_picker_nic
插件来显示一个国家代码选择器,并获取用户选择的国家代码、拨号代码和国家名称。你可以根据自己的需求进一步自定义和扩展这个示例。