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://stackoverflow.com/questions/58280192/animationcontroller-can-we-pass-the-tickerprovider-vsync-to-an-other-class

posted @ 2021-11-18 15:43  R1cardo  阅读(1304)  评论(0编辑  收藏  举报