简体   繁体   中英

flutter : favorite button not change color when pressed

When I press it, my button doesn't change color Does not change color when pressed

return Padding(
                          padding: const EdgeInsets.only(right: 8),
                          child: CircleAvatar(
                            backgroundColor: Colors.red,
                            child: IconButton(
                                onPressed: () =>
                                    snapshot.data.docs.length == 0
                                        ? addToFavorite()
                                        : print("Already Added"),
                                icon: snapshot.data.docs.length == 0
                                    ? Icon(
                                        Icons.favorite_outline,
                                        color: Colors.white,
                                      )
                                    : Icon(
                                        Icons.favorite,
                                        color: Colors.white,
                                      )),
                          ),
                        );

Do you have a call to setState() in your addToFavorite() method?

If not, you must call this function when the IconButton is pressed to update your icon by reevaluating your conditions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM