我在 flutter 中創建一個可變圖示按鈕時遇到問題。我只是想讓它在我點擊它時改變顏色。為了創建一個標記為 fabourite 按鈕。有人可以幫我嗎?
class p1 extends StatefulWidget {
@override
_p1State createState() => _p1State();
}
class _p1State extends State<p1> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body:Stack(
children:<Widget>[
Image(
image:AssetImage("Image/Chowsun1.jpg"),
fit:BoxFit.cover,
width: double.infinity,
height: double.infinity,
),
Align(
alignment: Alignment.bottomCenter,
child: (
IconButton(
icon: Icon(
Icons.favorite,
color:Colors.white
),
onPressed: (){
Hive.box(FAVORITES_BOX);
}
)
)
)])
),
);
}
}
uj5u.com熱心網友回復:
初始化一個變數說, bool isFavourite = true;
并在代碼中進行以下更改:
IconButton
(
icon: Icon
(
Icons.favorite,
color: isFavourite ? Colors.red : Colors.white
),
onPressed: ()
{
setState(()
{
isFavourite = !isFavourite;
});
}
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364076.html
