Flutter选择器插件uipickers的使用
Flutter选择器插件uipickers的使用
uipickers
是一个插件库,提供了平台特定的日期、时间和选择器弹窗。它在iOS上使用原生的iOS 14 UIDatePicker
和类似SwiftUI的Picker
,而在其他平台上则使用相应的材料选择器。
使用
首先,在您的Dart代码中导入该库:
import 'package:uipickers/uipickers.dart';
然后选择以下其中一个组件:
- AdaptiveDatePicker - 允许选择日期或时间,并在小部件中显示所选日期。根据当前平台自动选择底层弹出窗口。
- AdaptivePicker - 允许从字符串列表中选择一个项目。根据当前平台自动选择底层弹出窗口。
- UIDatePicker - 允许选择日期、时间或日期加时间,并使用原生的iOS 14风格的
UIDatePicker
组件。 - UIPicker - 允许从字符串列表中选择一个项目。使用iOS原生组件(
UIButton + UIMenu
)来呈现类似SwiftUI的选择器弹窗。 - MaterialDatePicker - 允许选择日期、时间或日期加时间,并使用内置的材料对话框。
- MaterialPicker - 允许从字符串列表中选择一个项目。使用内置的
DropDownButton
。
AdaptiveDatePicker
AdaptiveDatePicker
用于选择日期或时间。它在其小部件中显示当前选定的日期/时间。在iOS上,它将使用原生的iOS 14风格的UIDatePicker
。initialDate
属性设置当前选定的日期,firstDate
是最早允许的日期,lastDate
是最晚允许的日期。onChanged
事件处理程序在用户从弹窗中选择一个项时被调用:
DateTime selectedDate = DateTime.now();
//...
AdaptiveDatePicker(
initialDate: selectedDate,
firstDate: DateTime.now(),
lastDate: DateTime.now().add(Duration(days: 10)),
onChanged: (date) { setState(() => selectedDate = date); },
)
警告: 该小部件的大小应该被约束。例如,可以将其包裹在一个SizedBox
中:
SizedBox(width: 150, height: 34,
child: AdaptiveDatePicker(
//...
)
)
为了显式地使用原生版本,只需将type
属性设置为cupertino
,或者替换AdaptiveDatePicker
为UIDatePicker
:
AdaptiveDatePicker(
type: AdaptiveDatePickerType.cupertino,
//...
)
tintColor
属性仅适用于UIDatePicker
。它可以更改高亮文本的颜色:
UIDatePicker(
tintColor: UIColors.red,
//...
)
还可以通过多种属性进行自定义,例如backgroundColor
、cornerRadius
、borderColor
等:
AdaptiveDatePicker(
backgroundColor: Colors.blue[50]!,
borderColor: Colors.blue[800]!,
borderWidth: 3,
cornerRadius: 4,
items: items,
value: selectedItem,
onChanged: (value) { setState(() => selectedItem = value); },
);
AdaptivePicker
AdaptivePicker
小部件允许根据操作系统自动选择底层实现。在iOS上,它将使用基于UIButton + UIMenu
的SwiftUI风格的选择器。value
属性设置当前选定的项目索引,onChanged
事件处理程序在用户从弹窗中选择一个项时被调用:
int selectedItem = 1;
var items = [ 'one', 'two', 'three' ];
//...
AdaptivePicker(
items: items,
value: selectedItem,
onChanged: (value) { setState(() => selectedItem = value); }
)
警告: 该小部件的大小应该被约束。例如,可以将其包裹在一个SizedBox
中:
SizedBox(width: 150, height: 34,
child: AdaptiveDatePicker(
//...
)
)
为了显式地使用原生版本,只需将type
属性设置为cupertino
,或者替换AdaptivePicker
为UIPicker
:
AdaptivePicker(
type: AdaptivePickerType.cupertino,
//...
)
还可以通过多种属性进行自定义,例如backgroundColor
、cornerRadius
、borderColor
等:
UIPicker(
backgroundColor: Colors.blue[50]!,
borderColor: Colors.blue[800]!,
borderWidth: 3,
cornerRadius: 4,
items: items,
value: selectedItem,
onChanged: (value) { setState(() => selectedItem = value); },
);
完整示例
以下是完整的示例代码,展示了如何在Flutter应用中使用AdaptiveDatePicker
和AdaptivePicker
。
import 'package:flutter/material.dart';
import 'package:uipickers/uipickers.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int selectedItem = 0;
DateTime selectedDate = DateTime.now();
final key1 = GlobalKey();
final key2 = GlobalKey();
final items = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Padding(
padding: const EdgeInsets.fromLTRB(100, 140, 20, 20),
child: Column(
children: [
SizedBox(
width: 150,
height: 34,
child: AdaptivePicker(
key: key1,
type: AdaptivePickerType.material,
items: items,
value: selectedItem,
dropDownItemTextColor: Colors.green,
onChanged: (val) {
setState(() {
selectedItem = val ?? 0;
selectedDate = DateTime.now()
.add(Duration(days: selectedItem));
});
},
),
),
const SizedBox(height: 12),
SizedBox(
width: 150,
height: 34,
child: AdaptiveDatePicker(
key: key2,
type: AdaptiveDatePickerType.material,
initialDate: selectedDate,
firstDate: DateTime.now(),
lastDate: DateTime.now().add(const Duration(days: 10)),
onChanged: (date) {
setState(() {
selectedDate = date;
selectedItem =
daysBetween(DateTime.now(), selectedDate);
});
},
),
),
],
),
),
),
);
}
int daysBetween(DateTime from, DateTime to) {
var fromD = DateTime(from.year, from.month, from.day);
var toD = DateTime(to.year, to.month, to.day);
return (toD.difference(fromD).inHours / 24).round();
}
}
更多关于Flutter选择器插件uipickers的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter选择器插件uipickers的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter应用中使用uipickers
选择器插件的一个简单示例。uipickers
插件提供了一系列方便的日期和时间选择器,这里我们将展示如何使用其中的日期选择器。
首先,确保你已经在pubspec.yaml
文件中添加了uipickers
依赖:
dependencies:
flutter:
sdk: flutter
uipickers: ^x.y.z # 请将x.y.z替换为最新的版本号
然后运行flutter pub get
来安装依赖。
接下来,在你的Flutter应用中,你可以按照以下方式使用日期选择器:
import 'package:flutter/material.dart';
import 'package:uipickers/ui_date_picker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter UIPickers Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String selectedDate = "";
void _pickDate() async {
final DateTime? pickedDate = await UIDatePicker.showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(DateTime.now().year - 100),
lastDate: DateTime(DateTime.now().year + 100),
locale: Localizations.localeOf(context).toString(),
);
if (pickedDate != null && pickedDate != DateTime.now()) {
setState(() {
selectedDate = "${pickedDate.year}-${pickedDate.month}-${pickedDate.day}";
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('UIPickers Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Selected Date: $selectedDate',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _pickDate,
child: Text('Pick Date'),
),
],
),
),
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个按钮,点击该按钮将显示一个日期选择器。用户选择的日期将显示在按钮下方的文本中。
以下是代码的关键部分解释:
- 依赖添加:在
pubspec.yaml
中添加uipickers
依赖。 - 日期选择器调用:在
_pickDate
函数中,我们使用UIDatePicker.showDatePicker
方法来显示日期选择器。 - 更新状态:如果用户选择了一个日期,我们将更新
selectedDate
状态变量,并刷新UI以显示选择的日期。
这个示例展示了如何使用uipickers
插件中的日期选择器,你可以根据需要进一步自定义和扩展它。