Search code examples
listviewbuttonflutterdart

How to create a like button like using listview in flutter?


I am creating a blog app in flutter and stuck in a like button functionality. I want to do like(If a user presses the heart button in flutter listview, it will become red and when press again it will go in the original state) but when I click on the like button, color changes for all the buttons in the listview. Here is my code snippet.

body: ListView.builder(
      itemCount: 10,
      physics: BouncingScrollPhysics(),
      itemBuilder: (context, index) {
        return Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(3.0, 3.0, 3.0, 0.0),
              child: singleItem(index),
            ),
            Divider(height: 0.5, color: Constants.greyColor),
          ],
        );
      },
    ),
Widget singleItem(int index) {
return ListTile(
  leading: CircleAvatar(
    radius: 25.0,
    foregroundColor: Constants.orangeColor,
    backgroundColor: Constants.orangeColor,
    backgroundImage:
        NetworkImage("https://png.pngtree.com/svg/20161113/ef1b24279e.png"),
  ),
  trailing:
      Text("Jul23", style: TextStyle(color: Constants.ligthGreyColor)),
  title: Text("Jea @jeaBooty.jul23",
      style: TextStyle(
          color: Constants.slightBlackColor,
          fontSize: 16.0,
          fontWeight: FontWeight.w600)),
  subtitle: Container(
    margin: const EdgeInsets.symmetric(vertical: 8.0),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text("Happy Birthday, hope you will have a wonderful Day"),
        Container(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              IconButton(
                onPressed: () {},
                icon: Icon(Constants.commentIcon,
                    color: Constants.ligthGreyColor, size: 15.0),
              ),
              SizedBox(width: 50.0),
              IconButton(
                onPressed: () {
                  if (!_isLike) {
                    setState(() {
                      _isLike = true;
                      color = Constants.orangeColor;
                    });
                  } else {
                    setState(() {
                      _isLike = false;
                      color = Constants.ligthGreyColor;
                    });
                  }
                },
                icon: Icon(Constants.crownIcon, color: color, size: 15.0),
              ),
            ],
          ),
        ),
      ],
    ),
  ),
);

}


Solution

  • I guess as George Herbet mentioned in the comment, it seems like the same variable_isLike and color are used for all indices.

    Perhaps you can keep the state of isLike stored in a List instead, i.e. List<bool> _likes and then access the current one using the index, such as

    ...
    IconButton(
       onPressed: () {
           setState(() {
               _likes[index] = !_likes[index];
           });
           }
       },
       icon: Icon(Constants.crownIcon, color: _likes[index] ? Constants.orangeColor : 
    Constants.ligthGreyColor, size: 15.0),
    ),
    ...