2017-07-26 74 views
1

我试图在动画完成后执行动作。我尝试添加一个statusListener,但这不适合我。我的代码如下所示:试听动画以完成

@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的那部分代码如下所示:

child: new AnimatedBuilder(
    animation: _controller, 
    child: new Image.network(widget.url), 
    builder: (BuildContext context, Widget child) { 
    return new Transform.rotate(
     angle: _controller.value * 2.0 * math.PI, 
     child: child, 
    ); 
    }, 
), 

回答

1

对你的评论和编辑作出反应我看着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

按照实施例扑画廊的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(); 
}); 

我没有测试过这种无线你的代码。只是喊,如果它不工作;)

+0

我忘了说我正在使用'AnimatedBuilder'(我编辑了我的文章并添加了一些代码)。我认为我不能将你的方法应用到我写的东西上,还有其他想法吗? –