[英]Flutter, Flare: Execute function on end of animation
I have a simple question but can't find an answer to it: Is there a way to execute a function after my Flare animation is finished?我有一个简单的问题,但找不到答案:在我的 Flare animation 完成后,有没有办法执行 function? I have an animation that is shown on the app launch and I want the home screen to be shown after the animation is over.
我有一个在应用启动时显示的 animation,我希望在 animation 结束后显示主屏幕。 How can I do this?
我怎样才能做到这一点? I tried using Future.delayed() but don't know where to put the function.
我尝试使用 Future.delayed() 但不知道将 function 放在哪里。 If I put it into the builder of the StartAnimation widget, the EnterExitRoute is executed over and over again.
如果我将它放入 StartAnimation 小部件的构建器中,则会一遍又一遍地执行 EnterExitRoute。
import 'package:flutter/material.dart';
import 'animations.dart';
import 'package:flare_flutter/flare_actor.dart';
String _animationName = "Start";
Center(
child: GestureDetector(
onTap: () {
Navigator.push(context,
EnterExitRoute(exitPage: this, enterPage: HomeScreen()));
},
child: Column(
children: <Widget>[
Center(
child: Container(
height: 875,
child: FlareActor(
'src/animation.flr',
animation: _animationName,
fit: BoxFit.contain,
),
),
),
],
),
),
),
The FlareActor
widget has a callback parameter for that: FlareActor
小部件有一个回调参数:
/// Callback invoked when [animation] has completed. If [animation] is looping
/// this callback is never invoked.
final FlareCompletedCallback callback;
Other alternative is to use a FlareControls
that has a method onCompleted(String name)
:其他替代方法是使用具有
onCompleted(String name)
方法的FlareControls
:
/// Listen for when the animation called [name] has completed.
void onCompleted(String name) {}
For those looking for the callback对于那些寻找回调的人
child: FlareActor(
'src/animation.flr',
animation: _animationName,
fit: BoxFit.contain,
callback: (animationName) {
// Your code here. This is executed when the animation ends.
// If the animation is looping, then it will never be called.
},
),
The final code, working:最终代码,工作:
import 'package:flutter/material.dart';
import 'animations.dart';
import 'package:flare_flutter/flare_actor.dart';
String _animationName = "Start";
bool _appStart = true;
[in build method]
if (_appStart == true) {
Navigator.push(context,
EnterExitRoute(exitPage: this, enterPage: HomeScreen()));
_appStart = false;
}
[end of build method]
Center(
child: Column(
children: <Widget>[
Center(
child: Container(
height: 875,
child: FlareActor(
'src/animation.flr',
animation: _animationName,
fit: BoxFit.contain,
),
),
),
],
),
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.