如何在库比蒂诺日期选择器字段中显示初始日期
How to show initial date in Cupertino Date Picker Field
这是一个带有 Cupertino 日期选择器的小表格。
我想在表格上显示初始日期。点击文本字段时,它会显示一个日期选择器,一旦选择了新日期,它就会显示新日期
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
const double _kPickerSheetHeight = 216.0;
class AddCashPage extends StatefulWidget {
@override
_AddCashPageState createState() => _AddCashPageState();
}
class _AddCashPageState extends State<AddCashPage> {
final _formKey = GlobalKey<FormState>();
DateTime date = DateTime.now();
DateTime newDateTime;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Form(
key: _formKey,
child: buildTextFormField(),
),
],
),
);
}
Widget buildTextFormField() {
return Column(
children: <Widget>[
GestureDetector(
onTap: () {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) {
return _buildBottomPicker(
CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
initialDateTime: date,
onDateTimeChanged: (DateTime addSelectedDate) {
setState(() {
this.newDateTime = addSelectedDate;
date = newDateTime;
});
},
),
);
},
);
},
child: _buildMenu(
<Widget>[
//Show initial date here
Text(
DateFormat.yMMMMd().format(newDateTime),
),
],
),
),
],
);
}
}
我已将 date = newDateTime;
添加到 setState
,但 Text(DateFormat.yMMMMd().format(newDateTime),)
仍然显示为空。我在考虑使用三元运算符或者是否有更好的方法
使用initState
方法设置newDateTime
的初始值
void initState() {
super.initState();
newDateTime = DateTime.now();
}
这是一个带有 Cupertino 日期选择器的小表格。 我想在表格上显示初始日期。点击文本字段时,它会显示一个日期选择器,一旦选择了新日期,它就会显示新日期
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
const double _kPickerSheetHeight = 216.0;
class AddCashPage extends StatefulWidget {
@override
_AddCashPageState createState() => _AddCashPageState();
}
class _AddCashPageState extends State<AddCashPage> {
final _formKey = GlobalKey<FormState>();
DateTime date = DateTime.now();
DateTime newDateTime;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Form(
key: _formKey,
child: buildTextFormField(),
),
],
),
);
}
Widget buildTextFormField() {
return Column(
children: <Widget>[
GestureDetector(
onTap: () {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) {
return _buildBottomPicker(
CupertinoDatePicker(
mode: CupertinoDatePickerMode.date,
initialDateTime: date,
onDateTimeChanged: (DateTime addSelectedDate) {
setState(() {
this.newDateTime = addSelectedDate;
date = newDateTime;
});
},
),
);
},
);
},
child: _buildMenu(
<Widget>[
//Show initial date here
Text(
DateFormat.yMMMMd().format(newDateTime),
),
],
),
),
],
);
}
}
我已将 date = newDateTime;
添加到 setState
,但 Text(DateFormat.yMMMMd().format(newDateTime),)
仍然显示为空。我在考虑使用三元运算符或者是否有更好的方法
使用initState
方法设置newDateTime
的初始值
void initState() {
super.initState();
newDateTime = DateTime.now();
}