[英]How do I prevent the PopupMenu from disappearing when I press space bar within the TextField in it in Flutter Web?
[英]In Flutter, I want to get current date with button press and show in a Textfield, How can I do this?
我想通过按下“获取日期”按钮获取当前日期并显示在文本字段中,如下图所示,这可能吗? 我怎样才能做到这一点 ? 请在这里帮助新手....
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);
},
),
)
],
)
],
),
),
),
);
}
}
我想使用按钮“获取日期”获取当前日期并在文本字段中显示,如下图所示,
首先,将您的控制器分配给所需的 TextField,如controller: amount
。
其次,添加amount.text = '$date';
在你的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.