Flutter 如何禁用按钮本身
当app start button为enable时,我无法在其自身功能中禁用该按钮,当我单击该按钮时,_enable1=true,该按钮应为disable,但对我无效 这是我试过的代码Flutter 如何禁用按钮本身,flutter,dart,Flutter,Dart,当app start button为enable时,我无法在其自身功能中禁用该按钮,当我单击该按钮时,_enable1=true,该按钮应为disable,但对我无效 这是我试过的代码 if(_enable1) { resendbutoonfunction=() { print("hello"); setState(() { _enable1=false; mycolo=Colors.grey; }); // tim
if(_enable1) {
resendbutoonfunction=() {
print("hello");
setState(() {
_enable1=false;
mycolo=Colors.grey;
});
// timer = Timer.periodic(Duration(seconds: 5), (Timer t) =>disable());
// _controller.forward(from: 0.0);
// timer = Timer.periodic(Duration(seconds: 4), (Timer t) =>timertest());
};
}
这是我的按钮代码
onPressed:resendbutoonfunction,
据我所知,在编写代码时,测试(if_enable1)以及函数的定义只执行一次 测试必须位于函数内部:
resendbutoonfunction=() {
if (_enable1) {
...
}
}
或者,如果代码定期执行,则可以更改resendbutoonfunction的定义
if(_enable1) {
resendbutoonfunction=() {
print("hello");
setState(() {
_enable1=false;
mycolo=Colors.grey;
});
// timer = Timer.periodic(Duration(seconds: 5), (Timer t) =>disable());
// _controller.forward(from: 0.0);
// timer = Timer.periodic(Duration(seconds: 4), (Timer t) =>timertest());
};
}
else {
resendbutoonfunction=() {
// do nothing
}
}
要禁用按钮,需要将其设置为null
然后,对于您的按钮:
onPressed: _enable1 ? resendbutoonfunction : null,
onPressed: _enable1 ? resendbutoonfunction : null,