當我滿足功能中的條件時,我想將按鈕的背景更改為預設的背景。我怎樣才能做到這一點?
buttonColorSwitch(){
const a = 5;
const b = a;
if (b==5){
// switch Colors.green
}
if (b != b) {
// switch Colors.red
}
else ()
{
// swtitch defaultColor (blue)
};
}
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
//primary:Colors.blueAccent(defaultColor), switch Colors.green, or Colors.red
),
uj5u.com熱心網友回復:
您可以使用狀態變數或方法回傳顏色
int count = 0;
Color buttonColorSwitch() {
if (count == 0) {
return Colors.cyanAccent;
} else if (count.isEven) {
return Colors.green;
} else {
return Colors.purple;
}
}
ElevatedButton(
onPressed: () {
setState(() {
count ;
});
},
style: ElevatedButton.styleFrom(
backgroundColor: buttonColorSwitch(),
),
child: Text("BTN"),
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/511256.html
標籤:扑功能镖按钮颜色
