当前位置:网站首页>浅识Flutter 基本组件之showDatePicker方法
浅识Flutter 基本组件之showDatePicker方法
2022-07-31 03:12:00 【阿大豆】
浅识Flutter 基本组件之showDatePicker 方法
showDatePicker ()方法用于弹出一个日期选择器对话框,该对话框有系统默认的样式,也可以通过builder属性设置自定义样式,它的常用属性及功能说明如下所示。
属性名 | 类型 | 功能说明 |
---|---|---|
context | BuildContext | 设置BuildContext |
initialDate | DateTime | 设置日期选择器打开时默认日期 |
firstDate | DateTime | 设置日期选择器可选择的起始日期 |
lastDate | DateTime | 设置日期选择器可选择的终止日期 |
local | Locale | 设置国际化,默认英文 |
selectableDayPredicate | bool | 设置日期选择器可选的日期 |
builder | Widget | 设置日期选择器主题.标题栏等样式 |
例如,页面上有一个Text组件和一个RaiseButton组件,当点击RaiseButton组件后,弹出日期选择器,在日期选择器上选择日期后,将日期显示在Text组件上。
showdata(context) {
showDatePicker(
context: context,
initialDate: DateTime.now(),
lastDate: DateTime.now(),
firstDate: DateTime(1900, 01, 01));
}
FloatingActionButton(
onPressed: () {
showdata(context);
},
backgroundColor: Colors.white,
child: Icon(Icons.watch, color: Colors.grey),
)
将选择日期显示出来
String data = DateTime.now().toString().substring(0,10);
Future<DateTime?> showData(context) async{
// async异步调用的方法 需要等showDatePicker执行完 加上await
DateTime? d =await showDatePicker(
context: context,
initialDate: DateTime.now(),
lastDate: DateTime.now(),
firstDate: DateTime(1900, 01, 01));
/*将选择日期显示出来*/
setState(() {
data= d.toString().substring(0,10);
});
}
完整代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class zhucepage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyState();
}
}
class MyState extends State {
List flag = [false, false, false];
List select = ['皇后', '华妃', '甄嬛'];
bool isChecked = false;
//定义一个弹出日期选择器的方法showdata
String data = DateTime.now().toString().substring(0,10);
Future<DateTime?> showData(context) async{
// async异步调用的方法 需要等showDatePicker执行完 加上await
DateTime? d =await showDatePicker(
context: context,
initialDate: DateTime.now(),
lastDate: DateTime.now(),
firstDate: DateTime(1900, 01, 01));
/*将选择日期显示出来*/
setState(() {
data= d.toString().substring(0,10);
});
}
// showData(context) {
// var d=showDatePicker(
// context: context,
// initialDate: DateTime.now(),
// lastDate: DateTime.now(),
// firstDate: DateTime(1900, 01, 01));
// /*将选择日期显示出来*/
// setState(() {
// data= d.toString().substring(0,10);
// });
//
// }
@override
Widget build(BuildContext context) {
Row row = Row(
children: <Widget>[
Text('选择你的阵营: '),
Text(select[0]),
Checkbox(
value: flag[0],
onChanged: (value) {
//setState更新值
setState(() {
flag[0] = value!;
});
},
),
Text(select[1]),
Checkbox(
value: flag[1],
onChanged: (value) {
//setState更新值
setState(() {
flag[1] = value!;
});
},
),
Text(select[2]),
Checkbox(
value: flag[2],
onChanged: (value) {
//setState更新值
setState(() {
flag[2] = value!;
});
},
),
],
);
/*创建多个CheckboxListTile*/
Column column = Column(
children: <Widget>[
CheckboxListTile(
value: this.isChecked,
/*设置主标题组件*/
title: Text(
'全选',
style: TextStyle(color: Colors.red),
),
/*设置副标题组件*/
subtitle: Text('全选表示自成一派'),
/*设置显示的小组件,与□所在位置相反*/
secondary: Icon(Icons.flag),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
onChanged: (bool? value) {
setState(() {
isChecked = value!;
for (int i = 0; i < flag.length; i++) {
flag[i] = isChecked;
}
});
},
),
CheckboxListTile(
title: Text(select[0]),
subtitle: Text('堕胎小分队'),
secondary: Icon(Icons.info),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
value: flag[0],
onChanged: (value) {
setState(() {
flag[0] = value;
});
}),
CheckboxListTile(
title: Text(select[1]),
subtitle: Text('世兰好苦队'),
secondary: Icon(Icons.info),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
value: flag[1],
onChanged: (value) {
setState(() {
flag[1] = value;
});
}),
CheckboxListTile(
title: Text(select[2]),
subtitle: Text('莞莞类卿队'),
secondary: Icon(Icons.info),
/*调整复选框和图标的位置*/
controlAffinity: ListTileControlAffinity.leading,
value: flag[2],
onChanged: (value) {
setState(() {
flag[2] = value;
});
}),
// Row(children: <Widget>[
// Text("选择进宫的日子: "),
// Text(data),
// ]),
/*点击一个按钮,弹出日期选择对话框*/
Row(children: <Widget>[
Text("选择进宫的日子: "),
Text(data+' '),
FloatingActionButton(
onPressed: () {
showData(context);
},
backgroundColor: Colors.white,
child: Icon(Icons.watch, color: Colors.grey ),
),
]),
],
);
return Scaffold(
appBar: AppBar(
title: Text('用户注册'),
centerTitle: true,
),
body: column,
floatingActionButton: FloatingActionButton(
onPressed: () {
String info = "你选择的阵营是:";
for (int i = 0; i < flag.length; i++) {
if (flag[i]) {
/*如果选项被选中*/
info = info + select[i] + ' ';
}
}
print(info);
},
tooltip: 'Increment',
child: Icon(Icons.save),
),
floatingActionButtonLocation:
FloatingActionButtonLocation.centerFloat, //浮标的位置
);
}
}
边栏推荐
- Automation strategies for legacy systems
- 【CocosCreator 3.5】CocosCreator get network status
- Graphical lower_bound & upper_bound
- Why is String immutable?
- The simulation application of common mode inductance is here, full of dry goods for everyone
- 加密公司向盗窃的黑客提供报价:保留一点,把剩下的归还
- StringJoiner in detail
- Compile Hudi
- 注解用法含义
- 冒泡排序、选择排序、直接插入排序、二分法查找
猜你喜欢
QML的使用
LeetCode简单题之找到和最大的长度为 K 的子序列
[Compilation principle] Design principle and implementation of recursive descent parsing
5. SAP ABAP OData 服务如何支持 $filter (过滤)操作
With 7 years of experience, how can functional test engineers improve their abilities step by step?
Detailed explanation of TCP (1)
7年经验,功能测试工程师该如何一步步提升自己的能力呢?
The simulation application of common mode inductance is here, full of dry goods for everyone
7. List of private messages
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
随机推荐
5. SAP ABAP OData 服务如何支持 $filter (过滤)操作
Local area network computer hardware information collection tool
分布式系统架构需要解决的问题
Uninstallation of mysql5.7.37 under CentOS7 [perfect solution]
大小端模式
IDEA 注释报红解决
品牌广告投放平台的中台化应用与实践
What skills do I need to learn to move from manual testing to automated testing?
Thesis framework of the opening report
Several common errors when using MP
VS QT——ui不显示新添加成员(控件)||代码无提示
JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply
原子操作 CAS
Modbus on AT32 MCUs
10. Redis implements likes (Set) and obtains the total number of likes
[Compilation principle] Lexical analysis program design principle and implementation
【C语言】表达式求值的一般方法
With 7 years of experience, how can functional test engineers improve their abilities step by step?
TCP详解(二)
CorelDRAW2022 streamlined Asia Pacific new features in detail