【问题标题】:AnimationController: Can we pass the TickerProvider vsync to an other class?AnimationController:我们可以将 TickerProvider vsync 传递给其他类吗?
【发布时间】:2019-10-08 04:27:37
【问题描述】:

我在 Flutter 中开发了一个应用程序,其中包含很多不同的动画。我想通过分离视图、逻辑(模型 BLoC)和动画来构造我的代码。对于这个问题,我尝试为我的 StatefulWidget 的不同类中的按钮声明多次相同的动画。

但是,我被卡住了,因为我必须将 TickerProvider 传递给我的动画类,而且我的做法不正确。

构造函数动画类

AppBloc(TickerProvider tickerProvider) {
    banimationController = AnimationController(
      vsync: tickerProvider,
      duration: Duration(milliseconds: 100),
      lowerBound: 0,
      upperBound: 0.05,
    );
}

声明

AppBloc(this);

我知道这可能不是正确的方法,我写了这段代码来说明我的问题。

我只想将我的动画声明分隔到另一个文件中。

【问题讨论】:

    标签: animation flutter ticker


    【解决方案1】:

    TickerProvider 是一个混合。您可以使用 with 关键字在一个类中使用多个 mixin。使用 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 的值传递。

    【讨论】:

    • 好的,我用 TickerProviderStateMixin 颠倒了.. 非常感谢!
    • TickerProvider 不是 mixin。它是TickerProviderStateMixin 实现的接口。所以这个 mixin 是 TickerProvider 但不是相反。
    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 2013-06-11
    • 2018-05-22
    • 2015-10-26
    • 1970-01-01
    • 2011-07-09
    • 2014-08-14
    • 1970-01-01
    相关资源
    最近更新 更多