在Flutter中,如何通过点击另一个按钮来启用和禁用一个按钮



Row小部件中有两个提升的按钮。当我按下按钮1时,我想禁用按钮2,当我再次按下按钮1后,我想启用按钮2。我到处找,但似乎找不到答案。我希望有人能给我指明正确的方向。

您可以使用onPressed : null来禁用按钮2。

使用布尔变量设置禁用和启用。

var isDisable=true;

这是按钮1小部件,它使用setButton()方法来启用和禁用按钮2。

//button 1
RaisedButton( 
padding: const EdgeInsets.all(20),
textColor: Colors.white,
color: Colors.green,
onPressed:  (){
setState((){
setButton();
});
},
child: Text('Button 1'),
)

这是按钮2小部件,我们检查布尔变量(isDisable(以检查按钮2中的启用和禁用状态。

// button 2
RaisedButton(
padding: const EdgeInsets.all(20),
textColor: Colors.white,
color: Colors.green,
onPressed:  isDisable
? () => null : clickButton(),
child: Text('Button 2'),
)

这是一个用于更改启用和禁用按钮2的布尔变量值的方法。

//Method for enable and disable button2
void setButton(){
if(isDisable){
isDisable = false;
}else{
isDisable = true;
}

}

希望这能解决你的问题!

相关内容

最新更新