首页 > 解决方案 > 如何在单击按钮2时更改按钮1的背景颜色,反之亦然

问题描述

要求:我有两个按钮,一个是时间进入,另一个是超时。

启用时间的条件是

t2 != "" || t1 == ""

和超时启用条件是

t1 != ""

时间将被禁用时

t1 != "" && t2==""

并且超时将被禁用

t1==""

当我单击按钮时,其文本应更改为当前时间。

问题:一切都在一次正常工作,就像最初启用按钮的时间和禁用超时按钮一样,当我单击按钮中的时间时,它的文本和背景颜色会发生变化,而超时按钮会启用它的背景颜色也会发生变化,但是当我单击超时按钮时,它的文本会更改但按钮中的时间未启用,它仍然处于禁用状态,我想让它启用并且它的颜色应该改变并且与超时按钮相同,总之它应该执行反之亦然.

这是我所做的

var timeInText="                 Time in";
  var timeOutText="                 Time out";
  bool timeInBttonPressed=false;
  bool timeOutButtonPressed=false;
  var t1="";
  var t2="";

void initState() {
    // TODO: implement initState
    super.initState();
     _getTime();
}

Widget _timein() {
   //enable
    if(t2 != ""  || t1 == "" ) {
      return RoundedButton(icon: Icon(Icons.timer,color: Colors.white),
              text:timeInText,
              bgcolor: timeInBttonPressed ?Colors.blue[200]:Colors.blue[500],
              press: () {
                _getTime();
                setState(() {
                timeInText="                 "+getTime;
                t1=getTime;
                print("t1  "+t1);
                timeInBttonPressed=true;
                });
                
              });
    }
    //disable
      else if(t1 != "" && t2=="") {
      return RoundedButton(icon: Icon(Icons.timer,color: Colors.white),
              text:timeInText,
              bgcolor: Colors.blue[200],
              
              );
      } 
    }


     Widget _timeout(){
   //enable
      if(t1 != "") {
        return RoundedButton(icon: Icon(Icons.timer,color: Colors.white),
              text:timeOutText,
              bgcolor: timeOutButtonPressed ?Colors.blue[200]:Colors.blue[500],
              press: () {
                _getTime();
                setState(() {
                timeOutText="                 "+getTime;
                //t2=getTime;
                print("t2   "+t2);
                //timeOutButtonPressed=true;      
                timeInBttonPressed=true; 
                                });
                
        });

      }
      //disable
      else if(t1== "") {
        return RoundedButton(icon: Icon(Icons.timer,color: Colors.white),
              text:timeOutText,
              bgcolor: Colors.blue[200]);
      }
 }


void _getTime() {
    final String formattedDateTime =
        DateFormat('kk:mm:ss').format(DateTime.now()).toString();
    setState(() {
      getTime = formattedDateTime;
     print(getTime[0]);
    });
  }

注意:它基本上是一个考勤系统,其中用户登录时应该先做时间,当时间完成后才能超时。如果用户想再次计时,那么他可以,然后他必须在第二天再次计时。

希望一切都清楚,请帮助我解决我的问题。会非常感谢你。

标签: flutterbutton

解决方案


推荐阅读