在Flutter中更改Like按钮颜色

lmyy7pcs  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(172)

我试图改变图标的形状和颜色的图标按钮时,它被按下,但没有发生任何事情。下面是我的代码:

String adTitle = widget.documentSnapshot['Title'];
String $adDescription = widget.documentSnapshot['Description'];

IconData _iconData = Icons.favorite_border_outlined;
Color _iconColor = Colors.red;

return Scaffold(
  body: Column(
    children: [ 
      Row(
        children: [
          IconButton.outlined(
            icon: Icon(
              _iconData,
              size: 40,
              color: _iconColor,
            ),
            onPressed: () {
              setState(() {
                _iconData = Icons.favorite;
                _iconColor = Colors.blue;
              });
            },
          )
        ],
      ),
    ],
  ),
);
}
wtzytmuj

wtzytmuj1#

IconData _iconData = Icons.favorite_border_outlined;
Color _iconColor = Colors.red;

它们现在是局部变量。你需要把它们变成类级别的变量(在构建方法之外定义它们)

相关问题