Flutter:实现红包晃动效果

很多app打开会有红包悬浮在某个角落,然后为了吸引注意力,会将红包晃动起来,这个效果非常简单,代码如下:

class RedPackageSmall extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return _RedPackageSmall();
  }
}

class _RedPackageSmall extends State<RedPackageSmall> with SingleTickerProviderStateMixin{

  Animation _animation;
  AnimationController _animationController;

  @override
    void initState() {
      super.initState();
      _animationController = AnimationController(
        vsync: this,
        duration: Duration(milliseconds:200),
        reverseDuration: Duration(milliseconds: 200),
      );
      _animation = Tween(
        begin: -0.03,
        end: 0.03
      ).animate(_animationController);
    }
    
    @override
    void dispose() {
      _animationController.dispose();
      super.dispose();
    }

  @override
  Widget build(BuildContext context) {
    _animationController.repeat(reverse: true);
    return RotationTransition(
      turns: _animation,
      child: TextButton(
          onPressed: (){
              ...
          },
          child: Image.asset("红包图片"),
        ),
    );
  }

}

猜你喜欢

转载自blog.csdn.net/chzphoenix/article/details/122695666