美文网首页
动画(Animation)

动画(Animation)

作者: 梦幽辰 | 来源:发表于2020-02-02 14:17 被阅读0次
    class AnimationDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Animation'),
            elevation: 0,
          ),
          body: AnimationDemoHome(),
        );
      }
    }
    
    class _AnimationDemoHomeState extends State<AnimationDemoHome> with TickerProviderStateMixin {
      AnimationController animationDemoController;
      Animation animation;
      Animation animationColor;
      CurvedAnimation curve;
    
      @override
      void initState() {
        super.initState();
    
        animationDemoController = AnimationController(
          // value: 32,
          // lowerBound: 32,
          // upperBound: 100,
          duration: Duration(seconds: 1),
          vsync: this,
        );
    
        curve = 
            CurvedAnimation(parent: animationDemoController, curve: Curves.bounceOut);
    
        // Tween中只能使用double
        animation = Tween(begin: 32.0, end: 100.0).animate(curve);
        animationColor = 
            ColorTween(begin: Colors.red, end: Colors.red[900])
            .animate(curve);
    
        // animationDemoController.addListener(() {
        //   // print('${animationDemoController.value}');
        //   setState(() {});
        // });
    
        animationDemoController.addStatusListener((AnimationStatus status) {
          print(status);
        });
    
        // animationDemoController.forward(); // 开始播放动画
      }
    
      @override
      void dispose() {
        super.dispose();
    
        animationDemoController.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Center(
          child: AnimatedHeart(
            animations: [
              animation,
              animationColor
            ],
            controller: animationDemoController,
          )
        );
      }
    }
    
    class AnimatedHeart extends AnimatedWidget{
      final List animations;
      final AnimationController controller;
    
      AnimatedHeart({
        this.animations,
        this.controller,
      }): super(listenable: controller);
    
      @override
      Widget build(BuildContext context) {
        return IconButton(
            icon: Icon(Icons.favorite),
            iconSize: animations[0].value,
            color: animations[1].value,
            onPressed: () {
              switch (controller.status) {
                case AnimationStatus.completed:
                  controller.reverse(); // 反转播放动画
                  break;
                default:
                  controller.forward();
              }
            },
          );
      }
    }
    

    相关文章

      网友评论

          本文标题:动画(Animation)

          本文链接:https://www.haomeiwen.com/subject/tthfxhtx.html