启动画面-在initState上完成异步调用之前,AnimationController无法启动

时间:2020-01-27 22:41:38

标签: flutter dart flutter-animation

我正在尝试为Flutter应用程序创建启动画面。我希望我的徽标旋转,同时检查用户是否登录了Firebase身份验证,然后根据返回值转到相关视图。

问题是我的应用程序在异步调用之前无法正确构建(我看到了backGround但看不到AnimatedBuilder)。

我尝试使用after_layout程序包或使用此功能来运行CheckUser()

WidgetsBinding.instance.addPostFrameCallback((_) => yourFunction(context));

但是它总是等待CheckUser()函数完成,因此在直接导航到其他视图时我看不到动画。

如果要测试,这是我的代码:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:skull_mobile/connexion/login.dart';
import 'accueil.dart';

class SplashPage extends StatefulWidget {
  SplashPage({Key key}) : super(key: key);

  @override
  _SplashPage createState() => _SplashPage();
}

class _SplashPage extends State<SplashPage>
    with SingleTickerProviderStateMixin {

  AnimationController animationController;

  @override
  void initState() {
    super.initState();
    animationController = new AnimationController(
      vsync: this,
      duration: new Duration(seconds: 5),
    );
    animationController.repeat();
    checkUser();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[800],
      body: Center(
        child: Container(
          child: new AnimatedBuilder(
            animation: animationController,
            child: new Container(
              height: 150.0,
              width: 150.0,
              child: new Image.asset('assets/skull.png'),
            ),
            builder: (BuildContext context, Widget _widget) {
              return new Transform.rotate(
                angle: animationController.value * 6.3,
                child: _widget,
              );
            },
          ),
        ),
      ),
    );
  }

  void checkUser() async {
    FirebaseAuth.instance.currentUser().then((currentUser) => {
          if (currentUser == null)
            {Navigator.pushNamed(context, LoginPage.routeName)}
          else
            {Navigator.pushNamed(context, AccueilPage.routeName)}
        });
  }
}

1 个答案:

答案 0 :(得分:0)

在我的评论之后,我在这里分享了我自己的代码的片段以及如何处理启动屏幕(此处称为“ WaitingScreen”),设备的连接状态,然后将用户发送到具有不同属性的不同页面,具体取决于结果:

@override
Widget build(BuildContext context) {
  switch (authStatus) {
    case AuthStatus.notDetermined:
      if(_connectionStatus == ConnectionStatus.connected){
        return _buildWaitingScreen();
      }else{
        return _buildNoConnectionScreen();
      }
      break;
    case AuthStatus.notSignedIn:
      return LoginPage(
        onSignedIn: _signedIn,
        setThemePreference: widget.setThemePreference,
      );
    case AuthStatus.signedIn:
      return MainPage(
        onSignedOut: _signedOut,
        setThemePreference: widget.setThemePreference,
        getThemePreference: widget.getThemePreference,
      );
  }
  return null;
}