fluttercandies / like_button

Like Button is a flutter library that allows you to create a button with animation effects similar to Twitter's heart when you like something and animation effects to increase like count.
MIT License
469 stars 96 forks source link

Animation doesn't work when using "isLiked" parameter on the widget #69

Closed Infinitism closed 2 years ago

Infinitism commented 2 years ago

Hi,

Whenever I set the "isLiked" value on the widget, the animation stops working.

Scenario: I query the database to see what the current "like" state is and set it on the like_button. When user "un-hearts" it, db updates the like state which is reflected in the value I set for "isLiked" parameter for the widget.

Whenever "isLiked" is set to a value other than "false", the "un like" to "like" animation stops working.

` Expanded( child: LikeButton( isLiked: currentLikeState, mainAxisAlignment: MainAxisAlignment.end, size: 30, likeBuilder: (isLiked) { return isLiked ? Icon( Icons.favorite, color: Colors.pink, ) : Icon( Icons.favorite_outline, color: Colors.grey, ); }, onTap: (prevLikeState) async {

  /**
   * 1. Collect which entity was clicked on
   * 2. If not liked, add to user likes
   * 3. If already liked, remove from user likes
   */

  bool likeStatus = await db.updateEntityLike(!prevLikeState, entity, user);

  return likeStatus;

  // return db.updateEntityLike(!prevLikeState, entity, user);
},

), )

`

Infinitism commented 2 years ago

I found the solution to this.

Instead of checking the database on the current like state of an item and setting it on "isLiked" parameter, use the likeBuilder to render the appropriate "isLiked" widget based on the database value.

This will make sure that when the item is un-liked, the value is set to false in database, and when like again, the animation will happen as well!

zmtzawqlp commented 2 years ago

the same as https://github.com/fluttercandies/like_button/issues/51

erisanolasheni commented 1 year ago

And I think onTap should return not return isLiked, this should be !isLiked.