GIfatahTH / animator

A Flutter library that makes animation easer. It allows for separation of animation setup from the User Interface.
BSD 3-Clause "New" or "Revised" License
227 stars 28 forks source link

Animator key is not working whenever i am setting state of stateful Widget as well as trigger animation #41

Closed luciferamji closed 4 years ago

luciferamji commented 4 years ago

I have initialized it with a key directly in class final AnimatorKey animatorKey = AnimatorKey();

I am using it in a gesture Detector like this GestureDetector buildPostImage() { return GestureDetector( onDoubleTap: () { handleLikePost(); //trigger of animation }, child: Stack( alignment: Alignment.center, children: [ cachedNetworkImage( widget.mediaUrl, ), Animator( resetAnimationOnRebuild: true, animatorKey: animatorKey, duration: Duration(seconds: 1), tween: Tween(begin: 0.5, end: 1.5), curve: Curves.elasticInOut, cycles: 2, builder: (_, anim, __) => Transform.scale( scale: anim.value, child: Icon( Icons.favorite, size: 80.0, color: Colors.red[100], ), )), AnimatorRebuilder( observe: () => animatorKey, builder: (ctx, anim, child) => Transform.scale( scale: anim.value, child: Icon( Icons.favorite, size: 80.0, color: Colors.red[100], ), )) ], ), ); }

I am triggering animation from here void handleLikePost() { _isLiked = widget.likes.contains(widget.currentUserId);

if (_isLiked) {
  setState(() {                                                          //This is Working just fine as i remove setState 
    likeCount -= 1;                                                 //method.But i Want to run it with setState 
                                                                                method
    _isLiked = false;
    widget.likes.remove(widget.currentUserId);
  });
   animatorKey.triggerAnimation(restart: true);   //working if setState is not there    

} else {
  setState(() {
    likeCount += 1;
    _isLiked = true;
    widget.likes.add(widget.currentUserId);
  });

}

},

Thanx in advance for any help..

luciferamji commented 4 years ago

Hey Sorry it was a blunder.I forgot to set resetAnimationOnRebuid to false. Thats why It was not working while rebuilding.