I’m trying to save the state of an icon, it should change on click, but it only changes on hot reload, what’s my mistake?
IconButton –
IconButton(onPressed: () {
if (_favorite == Icons.favorite_border) {
_favorite = Icons.favorite;
print(1111);
} else {
_favorite = Icons.favorite_border;
print(2222);
}
}, icon: Icon(_favorite, color: configColors.orange, size: 10,)),
setState –
IconData _favorite = Icons.favorite_border;
void initState() {
_favorite = Icons.favorite_border;
super.initState();
}
>Solution :
Inside onPressed call the setState to update the UI for StatefulWidget
IconButton(onPressed: () {
if (_favorite == Icons.favorite_border) {
_favorite = Icons.favorite;
print(1111);
} else {
_favorite = Icons.favorite_border;
print(2222);
}
setState((){}); //here
}, icon: Icon(_favorite, color: configColors.orange, size: 10,)),