[英]How to change the color of raisedButton when selected
我想做一些非常簡單的事情,但我認為我錯過了一個步驟,因為它不起作用。 我需要做的是在選中時更改 raiseButton 的樣式。
到目前為止,我是這樣做的:
Container ChoiceChip(String chipName) {
bool isButtonPressed = false;
return Container(
child: RaisedButton(
color: isButtonPressed ? Colors.green : Colors.red,
child: Text(
chipName,
style: TextStyle(
color: Colors.black,
),
),
onPressed: () {
setState(() {
isButtonPressed = !isButtonPressed;
});
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
side: BorderSide(
color: isButtonPressed ? Colors.green : Colors.red,
),
),
),
);
}
如果我在 onPressed 上打印一些東西,它會顯示在終端中,但它不會改變按鈕的顏色
在 BuildContext 之前在無狀態/有狀態小部件中定義布爾值。 然后使用 initState 在啟動時存儲布爾值的狀態。 最后確保您完全重新啟動應用程序以重新初始化應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.