Flutter国家选择器插件country_picker的使用
Flutter国家选择器插件country_picker的使用
Country picker
country_picker
是一个Flutter包,用于从国家列表中选择一个国家。
Getting Started
添加依赖
在您的 pubspec.yaml
文件中添加以下内容:
dependencies:
country_picker: ^2.0.27
导入库
在您的Dart文件中导入该库:
import 'package:country_picker/country_picker.dart';
显示国家选择器
使用 showCountryPicker
方法来显示国家选择器:
showCountryPicker(
context: context,
showPhoneCode: true, // 可选。在国家名称前显示电话区号。
onSelect: (Country country) {
print('Select country: ${country.displayName}');
},
);
国际化支持
为了实现国际化,您需要在应用程序的本地化代理列表中添加 CountryLocalizations.delegate
:
MaterialApp(
supportedLocales: [
const Locale('en'),
const Locale('el'),
const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), // 简体中文 'zh_Hans'
const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant'), // 繁体中文 'zh_Hant'
],
localizationsDelegates: [
CountryLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: HomePage(),
);
参数说明
onSelect
: 当选择了一个国家时调用。国家选择器将新值传递给回调函数(必需)。onClosed
: 当关闭国家选择器时调用,无论是否选择了国家(可选)。showPhoneCode
: 是否在国家名称前显示电话区号(可选)。searchAutofocus
: 是否初始展开虚拟键盘(可选)。showSearch
: 是否显示/隐藏搜索栏(可选)。showWorldWide
: 是否在列表开头显示"World Wide"选项(可选)。favorite
: 在列表顶部显示喜欢的国家(可选)。moveAlongWithKeyboard
: 是否在文本框聚焦时移动底部弹出层以适应键盘(可选)。countryListTheme
: 自定义国家列表的样式(可选)。exclude
: 从国家列表中排除一个或多个国家(可选)。countryFilter
: 过滤国家列表(可选),不能同时提供exclude
和countryFilter
。
自定义主题示例
showCountryPicker(
context: context,
countryListTheme: CountryListThemeData(
flagSize: 25,
backgroundColor: Colors.white,
textStyle: TextStyle(fontSize: 16, color: Colors.blueGrey),
bottomSheetHeight: 500, // 可选。设置国家列表模态框的高度
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
),
inputDecoration: InputDecoration(
labelText: 'Search',
hintText: 'Start typing to search',
prefixIcon: const Icon(Icons.search),
border: OutlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFF8C98A8).withOpacity(0.2),
),
),
),
),
onSelect: (Country country) => print('Select country: ${country.displayName}'),
);
排除某些国家示例
showCountryPicker(
context: context,
exclude: <String>['KN', 'MF'], // 提供国家代码(iso2)列表
onSelect: (Country country) => print('Select country: ${country.displayName}'),
);
示例代码
下面是一个完整的示例代码,展示了如何在一个简单的Flutter应用中使用 country_picker
插件:
import 'package:country_picker/country_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo for country picker package',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
supportedLocales: [
const Locale('en'),
const Locale('ar'),
const Locale('es'),
const Locale('de'),
const Locale('fr'),
const Locale('el'),
const Locale('et'),
const Locale('nb'),
const Locale('nn'),
const Locale('pl'),
const Locale('pt'),
const Locale('ru'),
const Locale('hi'),
const Locale('ne'),
const Locale('uk'),
const Locale('hr'),
const Locale('tr'),
const Locale('lv'),
const Locale('lt'),
const Locale('ku'),
const Locale('nl'),
const Locale('it'),
const Locale('ko'),
const Locale('ja'),
const Locale('id'),
const Locale('cs'),
const Locale('ht'),
const Locale('sk'),
const Locale('ro'),
const Locale('bg'),
const Locale('ca'),
const Locale('he'),
const Locale('fa'),
const Locale('da'),
const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), // 简体中文 'zh_Hans'
const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant'), // 繁体中文 'zh_Hant'
],
localizationsDelegates: [
CountryLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Demo for country picker')),
body: Center(
child: ElevatedButton(
onPressed: () {
showCountryPicker(
context: context,
exclude: <String>['KN', 'MF'],
favorite: <String>['SE'],
showPhoneCode: true,
onSelect: (Country country) {
print('Select country: ${country.displayName}');
},
moveAlongWithKeyboard: false,
countryListTheme: CountryListThemeData(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(40.0),
topRight: Radius.circular(40.0),
),
inputDecoration: InputDecoration(
labelText: 'Search',
hintText: 'Start typing to search',
prefixIcon: const Icon(Icons.search),
border: OutlineInputBorder(
borderSide: BorderSide(
color: const Color(0xFF8C98A8).withOpacity(0.2),
),
),
),
searchTextStyle: TextStyle(
color: Colors.blue,
fontSize: 18,
),
),
);
},
child: const Text('Show country picker'),
),
),
);
}
}
通过以上步骤和示例代码,您可以轻松地在Flutter项目中集成并使用 country_picker
插件。如果您有任何问题或需要进一步的帮助,请随时提问!
更多关于Flutter国家选择器插件country_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复