Flutter 在一段时间内更改按钮颜色

Flutter 在一段时间内更改按钮颜色,flutter,dart,Flutter,Dart,按下按钮时,我正试图更换按钮。此颜色将保持3秒钟,然后恢复正常颜色。例如,正常颜色为灰色,当按下按钮时,按钮将保持蓝色3秒钟,然后再次变为灰色。 这就是我所尝试的: _hasBeenPressed = false; onTap: () { setState((){ Timer(Duration(seconds: 3), (){ _hasBeenPressed = !_hasBeenPressed; print("inside

按下按钮时,我正试图更换按钮。此颜色将保持3秒钟,然后恢复正常颜色。例如,正常颜色为灰色,当按下按钮时,按钮将保持蓝色3秒钟,然后再次变为灰色。
这就是我所尝试的:

_hasBeenPressed = false;
onTap: () {
        setState((){
          Timer(Duration(seconds: 3), (){
            _hasBeenPressed = !_hasBeenPressed;
 print("inside" + _hasBeenPressed.toString()); //prints true
          });
          _hasBeenPressed = !_hasBeenPressed;
 print(_hasBeenPressed); // prints false
        });
      },
打印第二次打印后的“内部”打印时
这是装饰:

decoration: BoxDecoration(
          color: _hasBeenPressed ? Colors.grey[800] : Colors.blue , 
          borderRadius: BorderRadius.circular(60)
        ),

当我按下它一次,我得到了颜色的变化,但当定时器完成颜色不会变回灰色,只有我重新加载代码。

包装内部
\u hasBeenPressed=_已在设置状态中按下

您可以使用RawMaterial按钮来避免更改颜色的代码过多


在setState内部调用setState不是一件坏事吗?是的,但在这里,我们实际上并不是在外部setState内部调用它。计时器正在异步调用它。我不能使用材质按钮,因为应用程序不依赖于此,我使用手势检测器创建自定义按钮
    RawMaterialButton(
          child: Text('Hello'),
          shape: CircleBorder(),
          constraints: BoxConstraints.tightFor(width: 100, height: 100),
          onPressed: (){}, 
          highlightColor: Colors.greenAccent ---> mention the highLightColor
    )