Flutter国际化选择器插件csc_picker_i18n的使用
Flutter国际化选择器插件csc_picker_i18n的使用
csc_picker
一个用于显示国家、州和城市的Flutter包。此外,它还允许根据选定的内容选择国家、州和城市列表,并且可以搜索全球范围内的国家、州和城市。
![]() |
![]() |
如何使用
要使用此包,请在pubspec.yaml
文件中添加csc_picker
作为依赖项。
CSCPicker(
onCountryChanged: (value) {
setState(() {
countryValue = value;
});
},
onStateChanged:(value) {
setState(() {
stateValue = value;
});
},
onCityChanged:(value) {
setState(() {
cityValue = value;
});
},
),
你将在onChanged
函数中获得反馈。
参数
参数名称 | 类型 | 描述 |
---|---|---|
flagState | CountryFlag | 启用(与国家名称一起获取标志)/禁用(禁用标志)/仅在下拉菜单中显示标志(ShowInDropdownOnly) |
layout | Layout | 切换下拉布局(水平/垂直) |
showStates | Boolean | 启用/禁用州下拉菜单(true/false) |
showCities | Boolean | 启用/禁用城市下拉菜单(true/false) |
dropdownDecoration | BoxDecoration | 下拉框装饰以样式化你的下拉选择器 [可选参数](与disabledDropdownDecoration一起使用) |
disabledDropdownDecoration | BoxDecoration | 禁用下拉框装饰以样式化你的下拉选择器 [可选参数](与disabled下拉Decoration一起使用) |
selectedItemStyle | TextStyle | 更改所选项的样式 |
dropdownHeadingStyle | TextStyle | 更改下拉对话框标题样式 |
dropdownItemStyle | TextStyle | 更改下拉对话框项目样式 |
dropdownDialogRadius | double | 更改下拉对话框圆角 |
searchBarRadius | double | 更改搜索栏圆角 |
defaultCountry | CscCountry | 设置默认国家 |
disableCountry | Boolean | 禁用国家下拉菜单(注意:需与默认国家一起使用) |
countryFilter | List of CscCountry | 仅显示你想要的国家列表 |
countrySearchPlaceholder | String | 国家搜索字段的占位符 |
stateSearchPlaceholder | String | 州搜索字段的占位符 |
citySearchPlaceholder | String | 城市搜索字段的占位符 |
countryDropdownLabel | String | 国家下拉菜单的标签/标题 |
stateDropdownLabel | String | 州下拉菜单的标签/标题 |
cityDropdownLabel | String | 城市下拉菜单的标签/标题 |
示例
以下是一个完整的示例代码,展示如何使用csc_picker_i18n
进行国际化。
import 'package:csc_picker_i18n/csc_picker.dart';
import 'package:csc_picker_i18n/i18n/country_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
/// 这是一个国家、州和城市选择器的实现。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'CSC Picker',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
supportedLocales: [
Locale("en"), // 英文
Locale("de"), // 德语
Locale("ua"), // 乌克兰语
],
locale: Locale("de"), // 默认语言为德语
localizationsDelegates: [
CscCountryLocalizations.delegate, // 国际化代理
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: MyHomePage(title: 'CSC Picker'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
/// 用于存储国家、州和城市数据的变量。
String countryValue = "";
String? stateValue = "";
String? cityValue = "";
String? address = "";
[@override](/user/override)
Widget build(BuildContext context) {
GlobalKey<CSCPickerState> _cscPickerKey = GlobalKey();
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
height: 600,
child: Column(
children: [
/// 添加CSC选择器小部件到应用中
CSCPicker(
/// 启用或禁用州下拉菜单 [可选参数]
showStates: true,
/// 启用或禁用城市下拉菜单 [可选参数]
showCities: true,
/// 启用(与国家名称一起获取标志)/禁用(禁用标志)/仅在下拉菜单中显示标志 [可选参数]
flagState: CountryFlag.DISABLE,
/// 下拉框装饰以样式化你的下拉选择器 [可选参数](与disabledDropdownDecoration一起使用)
dropdownDecoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10)),
color: Colors.white,
border: Border.all(color: Colors.grey.shade300, width: 1)),
/// 禁用下拉框装饰以样式化你的下拉选择器 [可选参数](与disabled下拉Decoration一起使用)
disabledDropdownDecoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10)),
color: Colors.grey.shade300,
border: Border.all(color: Colors.grey.shade300, width: 1)),
/// 搜索框占位符
countrySearchPlaceholder: "Country",
stateSearchPlaceholder: "State",
citySearchPlaceholder: "City",
/// 下拉菜单标签
countryDropdownLabel: "Country",
stateDropdownLabel: "State",
cityDropdownLabel: "City",
/// 默认国家
// defaultCountry: CscCountry.India,
/// 禁用国家下拉菜单(注意:需与默认国家一起使用)
// disableCountry: true,
/// 国家过滤器 [可选参数]
// countryFilter: [CscCountry.India, CscCountry.United_States, CscCountry.Canada],
/// 所选项样式 [可选参数]
selectedItemStyle: TextStyle(
color: Colors.black,
fontSize: 14,
),
/// 下拉对话框标题样式 [可选参数]
dropdownHeadingStyle: TextStyle(
color: Colors.black,
fontSize: 17,
fontWeight: FontWeight.bold),
/// 下拉对话框项目样式 [可选参数]
dropdownItemStyle: TextStyle(
color: Colors.black,
fontSize: 14,
),
/// 对话框圆角 [可选参数]
dropdownDialogRadius: 10.0,
/// 搜索栏圆角 [可选参数]
searchBarRadius: 10.0,
/// 当国家被选中时触发
onCountryChanged: (value) {
setState(() {
print("Selected country: ${value}");
/// 将值存储到country变量中
countryValue = value!;
});
},
/// 当州被选中时触发
onStateChanged: (value) {
setState(() {
/// 将值存储到state变量中
stateValue = value;
});
},
/// 当城市被选中时触发
onCityChanged: (value) {
setState(() {
/// 将值存储到city变量中
cityValue = value;
});
},
),
/// 在Text小部件中打印新选择的国家、州和城市
TextButton(
onPressed: () {
setState(() {
address = "$cityValue, $stateValue, $countryValue";
});
},
child: Text("Print Data")),
Text(address ?? ""),
],
)),
),
);
}
}
更多关于Flutter国际化选择器插件csc_picker_i18n的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter国际化选择器插件csc_picker_i18n的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
csc_picker_i18n
是一个 Flutter 插件,用于选择国家、州和城市,并且支持国际化。它可以帮助你在应用中实现一个多级选择器,用户可以选择国家、州和城市,并且支持多种语言。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 csc_picker_i18n
插件的依赖:
dependencies:
flutter:
sdk: flutter
csc_picker_i18n: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
基本使用
以下是一个简单的示例,展示了如何使用 csc_picker_i18n
插件来选择国家、州和城市:
import 'package:flutter/material.dart';
import 'package:csc_picker_i18n/csc_picker_i18n.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('CSC Picker Example'),
),
body: Center(
child: CSCPickerI18n(
onCountryChanged: (value) {
print("Selected Country: $value");
},
onStateChanged: (value) {
print("Selected State: $value");
},
onCityChanged: (value) {
print("Selected City: $value");
},
),
),
),
);
}
}
参数说明
CSCPickerI18n
组件提供了多个参数,用于自定义选择器的行为:
onCountryChanged
: 当用户选择国家时的回调函数。onStateChanged
: 当用户选择州时的回调函数。onCityChanged
: 当用户选择城市时的回调函数。countrySearchPlaceholder
: 国家搜索框的占位符。stateSearchPlaceholder
: 州搜索框的占位符。citySearchPlaceholder
: 城市搜索框的占位符。countryDropdownLabel
: 国家下拉框的标签。stateDropdownLabel
: 州下拉框的标签。cityDropdownLabel
: 城市下拉框的标签。showStates
: 是否显示州选择器(默认为true
)。showCities
: 是否显示城市选择器(默认为true
)。country
: 初始选择的国家。state
: 初始选择的州。city
: 初始选择的城市。countryFilter
: 国家过滤器,可以限制显示哪些国家。stateFilter
: 州过滤器,可以限制显示哪些州。cityFilter
: 城市过滤器,可以限制显示哪些城市。
国际化
csc_picker_i18n
插件支持多种语言,你可以通过设置 locale
参数来指定语言。例如:
CSCPickerI18n(
locale: 'zh', // 设置为中文
onCountryChanged: (value) {
print("Selected Country: $value");
},
onStateChanged: (value) {
print("Selected State: $value");
},
onCityChanged: (value) {
print("Selected City: $value");
},
)