当前位置:网站首页>识Flutter 基本组件之showTimePicker 方法
识Flutter 基本组件之showTimePicker 方法
2022-07-31 03:12:00 【阿大豆】
@[TOC]( 浅识Flutter 基本组件之showTimePicker ()方法)
showTimePicker ()方法用于弹出一个时间选择器对话框,该对话框有系统默认的样式,也可以通过builder属性设置自定义样式。在用户选择时间后,返回一个TimeOfDay类型的数据。它的常用属性及功能说明如下表。
属性名 | 类型 | 功能说明 |
---|---|---|
context | BuildContext | 设置BuildContext |
initialTime | TimeOfDay | 设置时间选择器打开时默认时间 |
builder | Widget | 设置时间选择器主题.标题栏等样式 |
Row(children: <Widget>[
Text("左脚进门的时间: "),
Text(time+' '),
FloatingActionButton(
onPressed: () {
showTime(context);
},
backgroundColor: Colors.white,
child: Icon(Icons.access_time_outlined, color: Colors.grey ),
),
]),
//定义一个弹出日期选择器的方法showdata
String time = TimeOfDay.fromDateTime(DateTime.now()).toString();//取得当前系统的时间
Future<TimeOfDay?> showTime(context) async{
// async异步调用的方法 需要等showDatePicker执行完 加上await
TimeOfDay? t =await showTimePicker(context: context, initialTime: TimeOfDay.fromDateTime(DateTime.now()));
}
将选择的时间显示出来
/*将选择日期显示出来*/
setState(() {
String? apm=t?.period.toString()=='DayPeriod.am'?'上午':'下午';
time= apm+t.toString().substring(10,15);
});
完整代码
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
String time = TimeOfDay.fromDateTime(DateTime.now()).toString();//取得当前系统的时间
Future<TimeOfDay?> showTime(context) async{
// async异步调用的方法 需要等showDatePicker执行完 加上await
TimeOfDay? t =await showTimePicker(context: context, initialTime: TimeOfDay.fromDateTime(DateTime.now()));
/*将选择日期显示出来*/
setState(() {
String? apm=t?.period.toString()=='DayPeriod.am'?'上午':'下午';
time= apm+t.toString().substring(10,15);
});
}
// 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.apps_sharp, color: Colors.grey ),
),
]),
Row(children: <Widget>[
Text("左脚进门的时间: "),
Text(time+' '),
FloatingActionButton(
onPressed: () {
showTime(context);
},
backgroundColor: Colors.white,
child: Icon(Icons.access_time_outlined, 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, //浮标的位置
);
}
}
边栏推荐
- 想从手工测试转岗自动化测试,需要学习哪些技能?
- JetPack component Databinding
- Observer pattern
- SonarQube的BUG定义
- Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest
- Multilingual settings of php website (IP address distinguishes domestic and foreign)
- [Compilation principle] Lexical analysis program design principle and implementation
- Mysql 45讲学习笔记(二十五)MYSQL保证高可用
- Use of QML
- 7年经验,功能测试工程师该如何一步步提升自己的能力呢?
猜你喜欢
The simulation application of common mode inductance is here, full of dry goods for everyone
Mysql 45讲学习笔记(二十五)MYSQL保证高可用
接口测试关键技术
12 Disk related commands
TCP详解(二)
QML的使用
CefSharp入门-winform
【C语言】进制转换一般方法
想从手工测试转岗自动化测试,需要学习哪些技能?
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
随机推荐
观察者模式
Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
【C语言】三子棋(经典解法+一览图)
LeetCode中等题之分数加减运算
SQL injection Less47 (error injection) and Less49 (time blind injection)
CefSharp入门-winform
YOLOV5 study notes (2) - environment installation + operation + training
下载jar包的好地方
Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
6. Display comments and replies
Discourse Custom Header Links
The Map Entry understanding and application
学习DAVID数据库(1)
4. Sensitive word filtering (prefix tree)
els 方块向左移动条件判断
Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢
【C语言】预处理操作
C#远程调试
Key Technologies of Interface Testing
Chapter 9 SVM实践