Flutter TickerProvider使用

Flutter TickerProvider使用

  • 当需要使用Animation controller时,需要在控制器初始化时传递一个vsync参数,此时需要用到TickerProvider,例如

    class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
      Animation<double> _animation;
      AnimationController _animationController;
    
      GoogleSignIn _googleSignIn;
      GoogleSignInAccount _googleSignInAccount;
      GoogleSignInAuthentication _googleSignInAuthentication;
      FirebaseAuth _auth;
    
      @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();
    }
    }
    

    然后在vsync参数中将this穿进去即可

  • 但是SingleTickerProviderStateMixin只适用于单个AnimationController的情况,如需使用多个AnimationController,请使用TickerProviderStateMixin

参考:https://*.com/questions/58280192/animationcontroller-can-we-pass-the-tickerprovider-vsync-to-an-other-class

上一篇:18、重载(Overload)和重写(Override)有什么区别?


下一篇:spring整合Mybatis