在Flutter上倒数后如何激活和取消激活按钮?

问题描述 投票:0回答:1

我想在倒计时期间停用按钮。倒数计时完成后,按钮将自动激活。

我该怎么办?

flutter timer countdown
1个回答
0
投票

最简单的解决方案是在倒计时期间禁用您在onTapGesture按钮上执行的任何代码,然后仅在倒计时停止时才执行它。

编辑:

根据要求添加了代码:

 bool countDownComplete = false; //Global boolean variable


 void countdownFunc(){
     //this is sample countdown function
     for(int a= 0; a<a++;a<10){
         if(a=9){
            setState(() { 
            countDownComplete = true;
               //when a=9, countdown will complete, so then set               boolean to true
           });
         }
     }
 }


 RaisedButton(
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(18.0),
    side: BorderSide(color: Colors.red)),
  onPressed: () {
      if(countDownComplete){
        //execute code
      } //else do nothing
   },
  color: Colors.red,
  textColor: Colors.white,
  child: Text("Buy now".toUpperCase(),
    style: TextStyle(fontSize: 14)),
),
© www.soinside.com 2019 - 2024. All rights reserved.