简体   繁体   English

在 Flutter 中,我想通过按下按钮获取当前日期并显示在文本字段中,我该怎么做?

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

相关问题 当我在 Flutter Web 中的 TextField 中按空格键时,如何防止 PopupMenu 消失? - How do I prevent the PopupMenu from disappearing when I press space bar within the TextField in it in Flutter Web? 需要您的支持:当我按下收藏按钮时,我想在我的 Flutter 数据库中获取产品 ID - Need your HELD : I want to get the product ID in my Flutter database when I press the favorite button Flutter - 如何将按钮添加到在文本字段中粘贴所选文本? - Flutter - how can I add the button to Paste selected text in textfield? 我想在容器中显示过滤器列表,但它在 flutter 中作为新屏幕打开。 我该怎么做? - I want to show filter list in a container but its opening as a new screen in flutter. how i can do it? 我们如何在flutter中获取StatefulWidget的生命周期? 我想确定当前屏幕上小部件的生命周期而不是完整的应用程序 - How Can we get the lifecycle of StatefulWidget in flutter? I want to determine the lifecycle of widget on current screen not of complete app 我想将图像放入 Flutter 文本之间的文本字段中,我该如何实现? - I want to put image into textfield in between text in Flutter how i can impliment it? 我如何在flutter中获取世界各地的当前日期和时间 - How do i get the current date and time of places around the world in flutter 如何将文本字段的值居中(颤动) - How do I center the value of the textfield (flutter) 如何在 Flutter 中为 TextField 添加渐变? - How do I add a gradient to a TextField in Flutter? 如何在颤动中制作圆形的 TextField? - How can I make rounded TextField in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM