Flutter:按下时如何调用 Future 函数

Flutter: How to call a Future function when on pressed

我正在尝试在选择日期后实施显示时间选择器...我能够实施 showDatePicker 并且工作正常...然后我尝试通过以下修复来实施 ShowTime 选择器在这里 但在将代码放入我的项目后,我注意到当我单击应该调用 showDatePicker 的 iconButton 时什么都没有发生

我粘贴了下面的代码 下面的注释代码是我一直在使用的 ShowDateicker,直到我想实现 ShowTimePicker

Future _selectDayAndTime(BuildContext context) async {
    DateTime _selectedDay = await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2018),
        lastDate: DateTime(2030),
        builder: (BuildContext context, Widget child) => child);

    TimeOfDay _selectedTime = await showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
    );

    if (_selectedDay != null && _selectedTime != null) {
      //a little check
    }
    setState(() {
      _selectedDate = _selectedDay;
    });
    print('...');
  }

  // void _presentDatePicker() {
  //   showDatePicker(
  //           context: context,
  //           initialDate: DateTime.now(),
  //           firstDate: DateTime.now(),
  //           lastDate: DateTime(2050))
  //       .then((pickedDate) {
  //     if (pickedDate == null) {
  //       return;
  //     }
  //     setState(() {
  //       _selectedDate = pickedDate;
  //     });
  //   });
  //   print('...');
  // }

然后是下面的图标按钮代码

FlatButton(
                                      textColor: Theme.of(context).primaryColor,
                                      child: Icon(Icons.calendar_today),
                                      onPressed: () => _selectDayAndTime,
                                    ),

我后来阅读并理解了如何调用该函数...我做对了但遗漏了一些东西检查下面的代码

FlatButton(
                                      textColor: 
Theme.of(context).primaryColor,
                                      child: Icon(Icons.calendar_today),
                                      onPressed: () => 
_selectDayAndTime(context), // I forgot to add the 
(context) and that's what fixed the issue for me
                                    ),

现在一切正常