Wigets学习之日期时间
时间:2021-07-12 08:48:47
CalendarDatePicker
日期选择器
initialDate
:初始化日期(必填)firstDate
:可选的最早日期(必填)lastDate
:可选的最晚日期(必填)onDateChanged
:日期改变事件(必填)currentDate
:当前选择日期onDisplayedMonthChanged
:年月改变事件initialCalendarMode
:日期格式,默认为DatePickerMode.day(天选择)
,可选值DatePickerMode.year(年选择)
selectableDayPredicate
:可选择的日期,接收一个当前显示的日期参数,返回true
则表示当前日期可选,返回false
则当前日期不可选
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '日期选择',
home: Home(),
);
}
}
class Home extends StatefulWidget {
Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('日期选择'),
),
body: Column(
children: [
CalendarDatePicker(
initialDate: DateTime.now(),
firstDate: DateTime(1970),
lastDate: DateTime.now(),
selectableDayPredicate: (date) {
return date != DateTime(2021, 7, 1);
},
onDisplayedMonthChanged: (value) {
print('onDisplayedMonthChanged $value');
},
onDateChanged: (value) {
print(value);
},
)
],
),
);
}
}
YearPicker
年份选择器
firstDate
:可选的最早年份(必填)lastDate
:可选的最晚年份(必填)selectedDate
:已选的年份(必填)onChanged
:年份改变事件(必填)currentDate
:当前选择日期initialDate
:初始化日期
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '年份选择',
home: Home(),
);
}
}
class Home extends StatefulWidget {
Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('年份选择'),
),
body: Column(
children: [
Expanded(
child: YearPicker(
firstDate: DateTime(1970),
lastDate: DateTime.now(),
selectedDate: DateTime(2020),
onChanged: (value) {
print(value);
})),
],
),
);
}
}
showDatePicker
显示日期选择器
context
:上下文(必填)initialDate
:初始化日期(必填)firstDate
:可选的最早日期(必填)lastDate
:可选的最晚日期(必填)currentDate
:当前选择日期initialEntryMode
:日期面板模式,默认为DatePickerEntryMode.calendar(日期面板)
,可选DatePickerEntryMode.input(输入框)
selectableDayPredicate
:可选择的日期,接收一个当前显示的日期参数,返回true
则表示当前日期可选,返回false
则当前日期不可选helpText
:顶部标题的文本cancelText
:取消按钮的文本confirmText
:确定按钮的文本locale
:国际化配置useRootNavigator
:是否使用路由,默认为true
routeSettings
:路由配置initialDatePickerMode
:日期格式,默认为DatePickerMode.day(天选择)
,可选值DatePickerMode.year(年选择)
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '日期选择',
home: Home(),
);
}
}
class Home extends StatefulWidget {
Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('日期选择'),
),
body: Column(
children: [
Row(
children: [
Text('日期选择:'),
Expanded(
child: TextField(
readOnly: true,
decoration: InputDecoration(hintText: '请选择日期'),
onTap: () {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1970),
lastDate: DateTime.now(),
selectableDayPredicate: (date) {
return date != DateTime(2021, 7, 1);
},
helpText: '请选择一个日期',
cancelText: '取消',
confirmText: '确定',
useRootNavigator: true,
initialDatePickerMode: DatePickerMode.day);
},
))
],
),
],
),
);
}
}
showDateRangePicker
日期范围选择器
context
:上下文(必填)initialDateRange
:初始化日期范围firstDate
:可选的最早日期(必填)lastDate
:可选的最晚日期(必填)currentDate
:当前选择日期initialEntryMode
:日期面板模式,默认为DatePickerEntryMode.calendar(日期面板)
,可选DatePickerEntryMode.input(输入框)
selectableDayPredicate
:可选择的日期,接收一个当前显示的日期参数,返回true
则表示当前日期可选,返回false
则当前日期不可选helpText
:顶部标题的文本cancelText
:DatePickerEntryMode.input
模式下取消按钮的文本confirmText
:DatePickerEntryMode.input
模式下确定按钮的文本saveText
:DatePickerEntryMode.calendar
模式下确定按钮的文本fieldStartHintText
:DatePickerEntryMode.input
模式下开始日期的提示文本fieldEndHintText
:DatePickerEntryMode.input
模式下结束日期的提示文本fieldStartLabelText
:DatePickerEntryMode.input
模式下开始日期label
的文本fieldEndLabelText
:DatePickerEntryMode.input
模式下结束日期label
的文本locale
:国际化配置useRootNavigator
:是否使用路由,默认为true
routeSettings
:路由配置
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '日期范围选择',
home: Home(),
);
}
}
class Home extends StatefulWidget {
Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('日期范围选择'),
),
body: Column(
children: [
Row(
children: [
Text('日期范围选择:'),
Expanded(
child: TextField(
readOnly: true,
decoration: InputDecoration(hintText: '请选择日期范围'),
onTap: () {
showDateRangePicker(
context: context,
firstDate: DateTime(1970),
lastDate: DateTime.now(),
helpText: '请选择日期范围',
cancelText: '取消',
confirmText: '确定',
saveText: '确定',
fieldStartHintText: '请输入开始日期',
fieldEndHintText: '请输入结束日期',
fieldStartLabelText: '开始日期',
fieldEndLabelText: '结束日期');
},
))
],
),
],
),
);
}
}
CupertinoDatePicker
IOS 风格的日期时间选择器
onDateTimeChanged
:时间改变事件(必填)mode
:显示模式,默认为CupertinoDatePickerMode.dateAndTime(日期时间)
,可选:CupertinoDatePickerMode.time(时间)
、CupertinoDatePickerMode.date(日期)
initialDateTime
:初始化日期时间minimumDate
:最小日期minimumYear
:最小年份,默认为1
maximumYear
:最大年份minuteInterval
:分钟的粒度,必须是 60 的正整数因子,即一分钟可以分割为60/minuteInterval
份,默认为1
use24hFormat
:是否使用 24 小时制,默认为false
backgroundColor
:背景色
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '日期时间选择',
home: Home(),
);
}
}
class Home extends StatefulWidget {
Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('日期时间选择'),
),
body: Column(
children: [
Expanded(
child: CupertinoDatePicker(
onDateTimeChanged: (value) {
print(value);
},
initialDateTime: DateTime.now(),
minimumDate: DateTime(2021, 6),
use24hFormat: true,
backgroundColor: Colors.black38,
)),
],
),
);
}
}
showTimePicker
时间选择器
context
:上下文(必填)initialTime
:初始化时间useRootNavigator
:是否使用路由,默认为true
initialEntryMode
:时间面板模式,默认为TimePickerEntryMode.dial(时钟面板)
,可选DatePickerEntryMode.input(输入框)
helpText
:顶部标题的文本cancelText
:取消按钮的文本confirmText
:确定按钮的文本routeSettings
:路由配置
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '日期时间选择',
home: Home(),
);
}
}
class Home extends StatefulWidget {
Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('日期时间选择'),
),
body: Column(
children: [
Row(
children: [
Text('时间选择:'),
Expanded(
child: TextField(
readOnly: true,
decoration: InputDecoration(hintText: '请选择时间'),
onTap: () {
showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
helpText: '请选择时间',
cancelText: '取消',
confirmText: '确定');
},
))
],
),
],
),
);
}
}
CupertinoTimerPicker
IOS 风格的时间选择器
onTimerDurationChanged
:时间改变事件(必填)mode
:模式,默认为CupertinoTimerPickerMode.hms(时/分/秒)
,可选:CupertinoTimerPickerMode.hm(时/分)
、CupertinoTimerPickerMode.ms(分/秒)
initialTimerDuration
:初始化时间,默认为Duration.zero
minuteInterval
:分钟的粒度,必须是 60 的正整数因子,即一分钟可以分割为60/minuteInterval
份,默认为1
secondInterval
:秒的粒度,必须是 60 的正整数因子,即一分钟可以分割为60/secondInterval
份,默认为1
alignment
:对齐方式,默认为Alignment.center
backgroundColor
:背景色
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '日期时间选择',
home: Home(),
);
}
}
class Home extends StatefulWidget {
Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('日期时间选择'),
),
body: Column(
children: [
Expanded(
child: CupertinoTimerPicker(
onTimerDurationChanged: (value) {
print(value);
},
initialTimerDuration: Duration(hours: 12),
backgroundColor: Colors.orange,
)),
],
),
);
}
}