[英]In Flutter, I want to get current date with button press and show in a Textfield, How can I do this?
I want to get current date with button "Get Date" press and show in a Textfield as in the image below, Is it possible?我想通过按下“获取日期”按钮获取当前日期并显示在文本字段中,如下图所示,这可能吗? How can I do this ?
我怎样才能做到这一点 ? Pls help newbie here....
请在这里帮助新手....
class _Home2State extends State<Home2> {
DateTime date;
final amount = TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'mytest1',
home: Scaffold(
body: Card(
child: Column(
children: <Widget>[
TextField(
decoration: InputDecoration(labelText: 'Amount'),
),
Row(
children: <Widget>[
Expanded(
child: TextField(
// onChanged: ???,
decoration: InputDecoration(labelText: 'Date'),
),
),
RaisedButton(
color: Colors.blue,
child: Text(
'Get Date',
style: TextStyle(color: Colors.white),
),
onPressed: () => setState(
() {
date = new DateTime.now();
print(date);
},
),
)
],
)
],
),
),
),
);
}
}
I want to get current date with button "Get Date" press and show in a Textfield as in the image below,我想使用按钮“获取日期”获取当前日期并在文本字段中显示,如下图所示,
First, assign your controller to the desired TextField like controller: amount
.首先,将您的控制器分配给所需的 TextField,如
controller: amount
。
Second, add amount.text = '$date';
其次,添加
amount.text = '$date';
in your setState()
在你的
setState()
DateTime date;
final amount = TextEditingController();
@override
Widget build(BuildContext context) {
return Card(
child: Column(
children: <Widget>[
TextField(
decoration: InputDecoration(labelText: 'Amount'),
),
Row(
children: <Widget>[
Expanded(
child: TextField(
controller: amount,
decoration: InputDecoration(labelText: 'Date'),
),
),
RaisedButton(
color: Colors.blue,
child: Text(
'Get Date',
style: TextStyle(color: Colors.white),
),
onPressed: () => setState(
() {
date = DateTime.now();
amount.text = '$date';
},
),
)
],
)
],
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.