【问题标题】:How I do make my animation repeat when I click on it with animatedbuilder? flutter/dart当我使用animationbuilder单击动画时,如何使动画重复?颤动/飞镖
【发布时间】:2020-07-23 00:12:42
【问题描述】:

我正在开发一个自定义动画按钮。每次用户点击它时,我都想重复动画。因此,当用户单击它时,容器会扩大。并恢复正常大小。当用户再次点击它时,它会再次点击它。现在动画只是放大到定义的大小并停止。之后就什么都做不了了。

class CustomAnimation extends StatefulWidget {
  @override
  _CustomAnimationState createState() => _CustomAnimationState();
}

class _CustomAnimationState extends State<CustomAnimation> with SingleTickerProviderStateMixin {

  AnimationController _controller;

  @override
  void initState() {
    // TODO: implement initState

    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
    );
    _controller.addListener(() {
      setState(() {
        //do something
      });
    });
    _controller.forward();
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: AnimatedBuilder(
          animation: _controller.view,
          builder: (context,child){
            return Transform.scale(scale: _controller.value *.9,
                child: Container(
                  width: 200,
                  height: 200,
                  color: Colors.lightGreen[200],
                  child: Center(
                    child: Text('Animation test'),
                  ),
                ),
            );
          },
        ),

      )
    );
  }
}

【问题讨论】:

    标签: flutter animation dart vsync animationcontroller


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    您可以收听AnimationStatus.completed 并拨打_controller.reverse()
    并使用InkWell 调用_controller.forward();

     animation = Tween<double>(begin: 1.0, end: 1.2).animate(_controller)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              _controller.reverse();
            }
          });
    ...   
    return Transform.scale(
                scale: animation.value,
                child: InkWell(
                  onTap: () {
                    _controller.forward();
                  },      
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    class CustomAnimation extends StatefulWidget {
      @override
      _CustomAnimationState createState() => _CustomAnimationState();
    }
    
    class _CustomAnimationState extends State<CustomAnimation>
        with SingleTickerProviderStateMixin {
      AnimationController _controller;
      Animation<double> animation;
    
      @override
      void initState() {
        _controller =
            AnimationController(vsync: this, duration: Duration(seconds: 2));
        _controller.addListener(() {
          setState(() {
            //do something
          });
        });
    
        _controller.forward();
        animation = Tween<double>(begin: 1.0, end: 1.2).animate(_controller)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              _controller.reverse();
            }
          });
        super.initState();
      }
    
      @override
      void dispose() {
        _controller.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Center(
              child: AnimatedBuilder(
                animation: animation,
                builder: (context, child) {
                  return Transform.scale(
                    scale: animation.value,
                    child: InkWell(
                      onTap: () {
                        _controller.forward();
                      },
                      child: Container(
                        width: 200,
                        height: 200,
                        color: Colors.lightGreen[200],
                        child: Center(
                          child: Text('Animation test'),
                        ),
                      ),
                    ),
                  );
                },
              ),
            ));
      }
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: CustomAnimation(),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-15
      • 2019-06-10
      • 2020-08-11
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多