[英]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.