AnimationController:我们可以将 TickerProvider vsync 传递给另一个 class 吗?
AnimationController: Can we pass the TickerProvider vsync to an other class?
我在 Flutter 中开发了一个应用程序,其中包含很多不同的动画。我想通过分离视图、逻辑(模型 BLoC)和动画来构建我的代码。对于这个问题,我尝试在我的 StatefulWidget 的不同 class 中为按钮声明多次相同的动画。
但是,我被卡住了,因为我必须将 TickerProvider 传递给我的动画 class,但我的做法不正确。
构造函数动画class
AppBloc(TickerProvider tickerProvider) {
banimationController = AnimationController(
vsync: tickerProvider,
duration: Duration(milliseconds: 100),
lowerBound: 0,
upperBound: 0.05,
);
}
声明
AppBloc(this);
我知道这可能不是正确的方法,我写这段代码是为了说明我的问题。
我只想将我的动画声明放在另一个文件中。
TickerProvider 是一个混入。您可以使用 with 关键字在 class 中使用多个混入。使用 mixin uff TickerProvider 的最佳方法是将其与 with 关键字一起使用。
示例:
class _HomeState extends State<Home> with TickerProviderStateMixin {
Animation<double> _animation;
AnimationController _animationController;
GoogleSignIn _googleSignIn;
GoogleSignInAccount _googleSignInAccount;
GoogleSignInAuthentication _googleSignInAuthentication;
FirebaseAuth _auth;
// FacebookLoginResult _facebookLoginResult;
// FacebookLogin _facebookLogin;
// FirebaseUser facebookUser;
@override
void initState() {
super.initState();
_animationController =
AnimationController(vsync: this, duration: Duration(seconds: 4));
_animation = Tween<double>(begin: -1.0, end: 0.0).animate(CurvedAnimation(
parent: _animationController, curve: Curves.fastOutSlowIn));
_animationController.forward();
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget();
}
}
如果您以这种方式使用 TickelProvider,那么您只需将 this 作为 vsync 的值传递即可。
我在 Flutter 中开发了一个应用程序,其中包含很多不同的动画。我想通过分离视图、逻辑(模型 BLoC)和动画来构建我的代码。对于这个问题,我尝试在我的 StatefulWidget 的不同 class 中为按钮声明多次相同的动画。
但是,我被卡住了,因为我必须将 TickerProvider 传递给我的动画 class,但我的做法不正确。
构造函数动画class
AppBloc(TickerProvider tickerProvider) {
banimationController = AnimationController(
vsync: tickerProvider,
duration: Duration(milliseconds: 100),
lowerBound: 0,
upperBound: 0.05,
);
}
声明
AppBloc(this);
我知道这可能不是正确的方法,我写这段代码是为了说明我的问题。
我只想将我的动画声明放在另一个文件中。
TickerProvider 是一个混入。您可以使用 with 关键字在 class 中使用多个混入。使用 mixin uff TickerProvider 的最佳方法是将其与 with 关键字一起使用。
示例:
class _HomeState extends State<Home> with TickerProviderStateMixin {
Animation<double> _animation;
AnimationController _animationController;
GoogleSignIn _googleSignIn;
GoogleSignInAccount _googleSignInAccount;
GoogleSignInAuthentication _googleSignInAuthentication;
FirebaseAuth _auth;
// FacebookLoginResult _facebookLoginResult;
// FacebookLogin _facebookLogin;
// FirebaseUser facebookUser;
@override
void initState() {
super.initState();
_animationController =
AnimationController(vsync: this, duration: Duration(seconds: 4));
_animation = Tween<double>(begin: -1.0, end: 0.0).animate(CurvedAnimation(
parent: _animationController, curve: Curves.fastOutSlowIn));
_animationController.forward();
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget();
}
}
如果您以这种方式使用 TickelProvider,那么您只需将 this 作为 vsync 的值传递即可。