Home > Blockchain >  flutter ListView.builder make onPressed works for only the concern item
flutter ListView.builder make onPressed works for only the concern item

Time:09-17

I have a list of cards inside a ListView.builder and each card has a favorite IconButton which change it's color when clicked, but whenever i click on it all the favorite icon change their color too, i wanted to work on the concern item. Thanks.

bool isPressed = false;
 .
 .
 .
onPressed: () {
   setState(() {
   isPressed = true;
    });
 }

CodePudding user response:

In your item you add a field isFavorite as bool type. You change the value of isFavorite and handle color based on isFavorite.

onPressed: (value) {
   setState(() {
    productItem[index].isFavorite = value;
    });
 }

For the color part you will check:

color: productItem[index].isFavorite?Colors.pinkAccent: Colors.grey,
  • Related