Datetime 如何在Flatter中创建日期和时间的自定义列表视图?
我正在使用flatter开发一个待办应用程序,在“新任务”屏幕中,我想创建一个“提醒”按钮,显示两个自定义列表视图,用户可以从中选择似乎无限的日期和时间。创建该按钮的最佳方法是什么 您可以使用软件包Datetime 如何在Flatter中创建日期和时间的自定义列表视图?,datetime,listview,button,flutter,flutter-layout,Datetime,Listview,Button,Flutter,Flutter Layout,我正在使用flatter开发一个待办应用程序,在“新任务”屏幕中,我想创建一个“提醒”按钮,显示两个自定义列表视图,用户可以从中选择似乎无限的日期和时间。创建该按钮的最佳方法是什么 您可以使用软件包 并将日期格式化设置为所需的日期 代码片段 DateTimePickerWidget( minDateTime: DateTime.parse(MIN_DATETIME), maxDateTime: DateTime.parse(MAX_
并将日期格式化设置为所需的日期
代码片段
DateTimePickerWidget(
minDateTime: DateTime.parse(MIN_DATETIME),
maxDateTime: DateTime.parse(MAX_DATETIME),
initDateTime: DateTime.parse(INIT_DATETIME),
locale: DateTimePickerLocale.pt_br,
dateFormat: DATE_FORMAT,
pickerTheme: DateTimePickerTheme(
showTitle: false,
title: Container(
width: double.infinity,
height: 40.0,
alignment: Alignment.center,
child: Text('Date Time Picker Title'),
decoration: BoxDecoration(color: Color(0xFFc0ca33)),
),
backgroundColor: Color(0xFFf0f4c3),
),
onChange: (dateTime, selectedIndex) {
setState(() {
_dateTime = dateTime;
});
},
)
工作演示,设备语言为葡萄牙语(PT)巴西语
完整代码
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
///
/// @author dylan wu
/// @since 2019-05-10
class DateTimePickerInPage extends StatefulWidget {
DateTimePickerInPage({Key key}) : super(key: key);
@override
State<StatefulWidget> createState() => _DateTimePickerInPageState();
}
const String MIN_DATETIME = '2019-05-15 20:10:55';
const String MAX_DATETIME = '2019-07-01 12:30:40';
const String INIT_DATETIME = '2019-05-16 09:00:58';
const String DATE_FORMAT = 'MMMM-EEEE-dd,HH:mm';
class _DateTimePickerInPageState extends State<DateTimePickerInPage> {
DateTime _dateTime;
@override
void initState() {
super.initState();
_dateTime = DateTime.parse(INIT_DATETIME);
}
@override
Widget build(BuildContext context) {
TextStyle hintTextStyle =
Theme.of(context).textTheme.subhead.apply(color: Color(0xFF999999));
return Scaffold(
appBar: AppBar(title: Text("DateTimePicker In Page")),
body: Container(
decoration: BoxDecoration(color: Colors.white),
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
// min datetime hint
Padding(
padding: EdgeInsets.only(bottom: 8.0),
child: Row(
children: <Widget>[
Container(
width: 115.0,
child: Text('min DateTime:', style: hintTextStyle),
),
Text(MIN_DATETIME,
style: Theme.of(context).textTheme.subhead),
],
),
),
// max datetime hint
Padding(
padding: EdgeInsets.only(bottom: 8.0),
child: Row(
children: <Widget>[
Container(
width: 115.0,
child: Text('max DateTime:', style: hintTextStyle),
),
Text(MAX_DATETIME,
style: Theme.of(context).textTheme.subhead),
],
),
),
// init datetime hint
Padding(
padding: EdgeInsets.only(bottom: 8.0),
child: Row(
children: <Widget>[
Container(
width: 115.0,
child: Text('init DateTime:', style: hintTextStyle),
),
Text(INIT_DATETIME,
style: Theme.of(context).textTheme.subhead),
],
),
),
// date format
Padding(
padding: EdgeInsets.only(bottom: 8.0),
child: Row(
children: <Widget>[
Container(
width: 115.0,
child: Text('Date Format:', style: hintTextStyle),
),
Text(DATE_FORMAT, style: Theme.of(context).textTheme.subhead),
],
),
),
// show custom title widget
Row(
children: <Widget>[
Text('show custom title', style: hintTextStyle),
Checkbox(value: true, onChanged: (value) {}),
],
),
// custom title height
Padding(
padding: EdgeInsets.only(bottom: 8.0),
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 8.0),
child: Text('custom title height:', style: hintTextStyle),
),
Text('40.0', style: Theme.of(context).textTheme.subhead),
],
),
),
// date time picker widget
Container(
margin: EdgeInsets.only(top: 8.0, bottom: 40.0),
child: DateTimePickerWidget(
minDateTime: DateTime.parse(MIN_DATETIME),
maxDateTime: DateTime.parse(MAX_DATETIME),
initDateTime: DateTime.parse(INIT_DATETIME),
locale: DateTimePickerLocale.pt_br,
dateFormat: DATE_FORMAT,
pickerTheme: DateTimePickerTheme(
showTitle: false,
title: Container(
width: double.infinity,
height: 40.0,
alignment: Alignment.center,
child: Text('Date Time Picker Title'),
decoration: BoxDecoration(color: Color(0xFFc0ca33)),
),
backgroundColor: Color(0xFFf0f4c3),
),
onChange: (dateTime, selectedIndex) {
setState(() {
_dateTime = dateTime;
});
},
),
),
// selected date time
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Selected DateTime:',
style: Theme.of(context).textTheme.subhead),
Container(
width: double.infinity,
padding: EdgeInsets.only(top: 4.0),
child: Text(
_dateTime != null
? '${_dateTime.year}-${_dateTime.month.toString().padLeft(2, '0')}-${_dateTime.day.toString().padLeft(2, '0')} ${_dateTime.hour.toString().padLeft(2, '0')}:${_dateTime.minute.toString().padLeft(2, '0')}:${_dateTime.second.toString().padLeft(2, '0')}'
: '',
style: Theme.of(context).textTheme.title,
),
),
],
),
],
),
),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DateTimePickerInPage(),
);
}
}
导入“包装:颤振/材料.省道”;
进口“包装:flutter_-cupertino_-date_-picker/flutter_-cupertino_-date_-picker.dart”;
///
///@作者吴迪伦
///@自2019年5月10日起
类DateTimePickerPage扩展StatefulWidget{
DateTimePickerPage({Key}):超级(Key:Key);
@凌驾
State createState()=>\u DateTimePickerInPageState();
}
常量字符串最小日期时间='2019-05-15 20:10:55';
常量字符串MAX_DATETIME='2019-07-01 12:30:40';
常量字符串INIT_DATETIME='2019-05-16 09:00:58';
常量字符串日期\格式='MMMM EEEE dd,HH:mm';
类_DateTimePickerInPageState扩展状态{
DateTime _DateTime;
@凌驾
void initState(){
super.initState();
_dateTime=dateTime.parse(INIT_dateTime);
}
@凌驾
小部件构建(构建上下文){
text样式hintTextStyle=
Theme.of(context).textTheme.subhead.apply(颜色:color(0xFF999999));
返回脚手架(
appBar:appBar(标题:文本(“页面中的日期时间选择器”),
主体:容器(
装饰:盒子装饰(颜色:彩色。白色),
填充:所有边缘设置(16.0),
子:列(
儿童:[
//最小日期时间提示
填充物(
填充:仅限边缘设置(底部:8.0),
孩子:排(
儿童:[
容器(
宽度:115.0,
子项:文本('min DateTime:',样式:hintTextStyle),
),
文本(最小日期时间,
风格:Theme.of(context.textTheme.subhead),
],
),
),
//最大日期时间提示
填充物(
填充:仅限边缘设置(底部:8.0),
孩子:排(
儿童:[
容器(
宽度:115.0,
子项:文本('max DateTime:',样式:hintTextStyle),
),
文本(最大日期时间,
风格:Theme.of(context.textTheme.subhead),
],
),
),
//初始化日期时间提示
填充物(
填充:仅限边缘设置(底部:8.0),
孩子:排(
儿童:[
容器(
宽度:115.0,
子项:文本('init DateTime:',样式:hintTextStyle),
),
文本(初始日期时间,
风格:Theme.of(context.textTheme.subhead),
],
),
),
//日期格式
填充物(
填充:仅限边缘设置(底部:8.0),
孩子:排(
儿童:[
容器(
宽度:115.0,
子项:文本('日期格式:',样式:hintTextStyle),
),
文本(日期格式,样式:Theme.of(context).textTheme.subhead),
],
),
),
//显示自定义标题小部件
划船(
儿童:[
文本(“显示自定义标题”,样式:hintTextStyle),
复选框(值:true,onChanged:(值){}),
],
),
//自定义标题高度
填充物(
填充:仅限边缘设置(底部:8.0),
孩子:排(
儿童:[
容器(
页边距:仅限边集(右:8.0),
子项:文本('自定义标题高度:',样式:hintTextStyle),
),
文本('40.0',样式:Theme.of(context).textTheme.subhead),
],
),
),
//日期时间选择器小部件
容器(
边距:仅限边集(顶部:8.0,底部:40.0),
子项:DateTimePickerWidget(
minDateTime:DateTime.parse(MIN_DateTime),
maxDateTime:DateTime.parse(MAX_DateTime),
initDateTime:DateTime.parse(INIT_DateTime),
区域设置:DateTimePickerLocale.pt\u br,
dateFormat:DATE_格式,
选取主题:日期时间选取主题(
节目名称:假,
标题:集装箱(
宽度:double.infinity,
身高:40.0,
对齐:对齐.center,
子项:文本(“日期时间选择器标题”),
装饰:盒子装饰(颜色:颜色(0xFFc0ca33)),
),
背景颜色:颜色(0xFFf0f4c3),
),
onChange:(日期时间,选择索引){
设置状态(){
_dateTime=dateTime;
});
},
),
),
//选定日期时间
纵队(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
Text('所选日期时间:',
风格:Theme.of(context.textTheme.subhead),
容器(
宽度:double.infinity,
填充:仅限边缘设置(顶部:4.0),
子:文本(
_日期时间!=null
?“${u dateTime.year}-${u dateTime.month.toString().padLeft(2,'0')}-${{u dateTime.day.toString().padLeft(2,'0')}${{u dateTime.hour.toString().padLeft(2,'0'))