【问题标题】:Flutter: Help evaluating function and displaying output with button press in flutterFlutter:帮助评估功能并在 Flutter 中按下按钮显示输出
【发布时间】:2020-10-13 17:59:06
【问题描述】:

我想复制https://sleepyti.me/ 的应用程序这是我当前的布局:https://imgur.com/rby0aBN

我还没有完成所有功能,但在按下按钮时难以显示结果。我的功能是:

void CalcBedTime(){
  var bedArr = [];
  DateTime nowDate = DateTime.now();
  for(var i = 0; i >= 7; i++){
    var nowHour = nowDate.hour;
    var newHour1 = nowDate.add(Duration(minutes: 90));
    String formatDate1 = DateFormat.jm().format(newHour1);
    bedArr.add(formatDate1);
  }
  print(bedArr);
}

由于此当前函数没有时间输入,因此我目前正在处理如果您在当前时间入睡则需要时间唤醒的函数。我也不确定我是否应该保持函数 void 并让它打印返回数组或让它返回数组值。我的按钮是:

FlatButton(
                color: Colors.blue,
                textColor: Colors.white,
                disabledColor: Colors.grey,
                disabledTextColor: Colors.black,
                padding: EdgeInsets.all(8.0),
                splashColor: Colors.blueAccent,
                onPressed:  ()   {  
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => SleepNowPage(title: 'Sleep Now Page',)),
                  );

                  /*...*/
                },
                child: Text(
                  "I'm going to sleep now" ,
                  style: TextStyle(fontSize: 20.0),
                ),
              )

我不太确定如何或在哪里评估函数,然后如何给出要打印的返回数组。我的 SleepNowPage 有:

return Scaffold(
  appBar: AppBar(
   
    title: Text(widget.title),
  ),
  body: Center(
    
    child: Column(
      
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(
            'If you sleep now you should wake up at: \n '
        ),

      ],
    ),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: Icon(Icons.add),
  ), // This trailing comma makes auto-formatting nicer for build methods.
);

抱歉,如果这不容易理解,我是 Flutter 新手,希望能得到任何帮助。

【问题讨论】:

    标签: function flutter dart widget output


    【解决方案1】:

    试试这个功能:

      List<String> calcBedTime({
    int hour,
    int minute,
    int second,
    }) {
    int sleepCycle = 90;
    bool useCurrentTime;
    if (hour != null && minute != null && second != null) {
      useCurrentTime = false;
    } else {
      useCurrentTime = true;
    }
    DateTime nowDate = useCurrentTime
        ? DateTime.now()
        : DateTime.parse('2020-02-27 $hour:$minute:$second');
    
    List<DateTime> bedArr = [
      nowDate.add(Duration(minutes: sleepCycle * 1)),
      nowDate.add(Duration(minutes: sleepCycle * 2)),
      nowDate.add(Duration(minutes: sleepCycle * 3)),
    ];
    List<String> bedArrInHours =
        bedArr.map((item) => '${item.hour}:${item.minute}').toList();
    print(bedArrInHours);
    return bedArrInHours;
    }
    

    如果你想知道现在睡着的时间,只需使用:

    calcBedTime();
    

    结果将是:

    [19:54, 21:24, 22:54]
    

    如果你想在特定时间睡觉时醒来时间,只需传递可选参数(只需确保提供的值是两个字符长):

    calcBedTime(hour: 12 , minute: 30 , second: 10);
    

    注意:这个函数只返回 3 个睡眠时间,你可以像我一样通过硬编码来添加更多,或者更好的是使用非常简单的 for 循环来完成。 注意 2:2020:02:27 的日期并不重要,只是使用 DateTime.parse() 接受的格式。

    【讨论】:

    • 谢谢我非常感谢更新的方法。我的下一个问题是如何在按下按钮时评估该功能并将其显示在我的页面上?如果我评估按钮按下的功能,我如何获得返回消息以显示时间?谢谢你的帮助:)
    • 您可以接受函数的返回值并将其存储在 List 类型的变量中,然后在页面状态中创建另一个变量,然后使用 setState,现在您可以使用数据了,您可以随心所欲地使用。
    猜你喜欢
    • 2019-12-05
    • 2020-06-30
    • 2018-08-23
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2021-11-29
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多