听完动画

时间:2017-07-26 03:21:43

标签: animation dart flutter

我想在动画完成后尝试执行动作。我尝试添加一个statusListener,但这对我不起作用。我的代码如下所示:

Using MJRefresh (3.1.12)
Installing NIMSDK (4.0.0)
Using Reachability (3.1.1)
Using SDWebImage (3.8.2)
Using SSZipArchive (1.8.1)
Using SVProgressHud (2.0.4)
Using SnapKit (3.0.0)
Using SwiftyJSON (3.1.4)
Using TZImagePickerController (1.7.9)
Using Toast (3.1.0)
Using Zip (0.6.0)

[!] The 'Pods-GXTax' target has libraries with conflicting names: libcrypto.a and libssl.a

@override void initState() { super.initState(); _controller = new AnimationController( duration: new Duration(milliseconds: 500), vsync: this, )..addStatusListener((AnimationStatus status) { print("Going"); if (status.index == 3 && spins > 0) { // AnimationStatus index 3 == completed animation _controller.duration = new Duration(milliseconds: speed - 50); _controller.forward(from: _controller.value == null ? 0.0 : 1 - _controller.value); spins--; print(speed); } }); } 永远不会被执行但我的动画结束了。出了什么问题?

/// ---编辑--- ///

我使用print(Going);,代码的一部分如下所示:

AnimatedBuilder

3 个答案:

答案 0 :(得分:2)

对你的评论和编辑做出反应我查看了AnimationBuilder。调整docs中的示例我想出了这个有效的解决方案:

class Spinner extends StatefulWidget {
  @override
  _SpinnerState createState() => new _SpinnerState();
}

class _SpinnerState extends State<Spinner> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  CurvedAnimation _animation;

  @override
  void initState() {
    super.initState();
    _controller = new AnimationController(
      duration: const Duration(seconds: 5),
      vsync: this,
    )..forward();

    _animation = new CurvedAnimation(
        parent: _controller,
        curve: Curves.linear,
    )..addStatusListener((AnimationStatus status) {
      if (status == AnimationStatus.completed)
        print('completed');
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new AnimatedBuilder(
      animation: _animation,
      child: new Container(width: 200.0, height: 200.0, color: Colors.green),
      builder: (BuildContext context, Widget child) {
        return new Transform.rotate(
          angle: _controller.value * 2.0 * 3.1415,
          child: child,
        );
      },
    );
  }
}

正如您所看到的,我使用控制器作为动画的父级,而不是用作AnimationBuilder的动画。希望它有所帮助。

答案 1 :(得分:1)

按照flutter gallery progress indicators中的示例,您应该将StatusListener附加到动画,而不是控制器

_controller = new AnimationController(
  duration: const Duration(milliseconds: 1500),
  vsync: this,
)..forward();

_animation = new CurvedAnimation(
  parent: _controller,
  curve: const Interval(0.0, 0.9, curve: Curves.fastOutSlowIn),
  reverseCurve: Curves.fastOutSlowIn
)..addStatusListener((AnimationStatus status) {
  if (status == AnimationStatus.dismissed)
    _controller.forward();
  else if (status == AnimationStatus.completed)
    _controller.reverse();
});

我没有用你的代码测试过这个。如果它不起作用,请大声喊叫;)

答案 2 :(得分:0)

您还可以使用whenComplete监听器

_animationController.forward().whenComplete((){
     //Trigger different responses, when animation is started at different places.
})