有没有一种方法可以根据一天中的时间禁用按钮

时间:2020-11-01 20:31:43

标签: flutter time flutter-dependencies

所以从本质上讲,我想在2pm之前禁用一个checkout按钮,但似乎无法弄清楚如何做以及在何处放置if语句逻辑。

2 个答案:

答案 0 :(得分:0)

以下按钮将在凌晨1点至凌晨3点之间保持禁用状态。

RaisedButton(
      onPressed: (DateTime.now().hour >= 1 && DateTime.now().hour <= 3) ? null : () => whatToDoOnPressed,
      child: Text('Button text')
    );

以下按钮将保持禁用状态,直到下午2点

RaisedButton(
      onPressed: (DateTime.now().hour < 14) ? null : () => whatToDoOnPressed,
      child: Text('Button text')
    );

答案 1 :(得分:0)

您可以添加onTap或onPressed

onTap: (){
    // in this way everytime it press it will check the time, and not only one time on build!
    if (DateTime.now().hour < 14) return; 
    else checkout();
},

您可能想要对按钮的颜色进行相同的操作(无需重复代码; P)

但是,请注意: 时间是当地时间!因此,可能不是您的服务器之一!