加载应用程序时,如何禁止RegistrationPage出现短暂外观/闪烁?

时间:2019-04-01 04:45:07

标签: dart flutter flutter-layout

我有一个应用程序,如果有用户登录,我会在最开始的地方在RootPage上进行验证。每次启动应用程序时都会完成此操作。

如果他已登录->显示StartPage 如果未登录->显示RegistrationPage

现在,当应用程序加载时,RegistrationPage会短暂闪烁(猜测您在验证登录状态时可以看到几毫秒)。我不要

如何禁止RegistrationPage出现太短?

RootPage

import 'package:first_app/start_screen.dart';
import 'package:first_app/user_auth/registration_start_screen.dart';
import 'package:flutter/material.dart';
import 'package:first_app/user_auth/auth.dart';

class RootPage extends StatefulWidget {
  RootPage({this.auth, this.completed});

  final BaseAuth auth;
  final bool completed;

  @override
  State createState() => new RootPageState();
}

enum AuthStatus { notSignedIn, signedIn }

class RootPageState extends State<RootPage> {
  AuthStatus authStatus = AuthStatus.notSignedIn;

  @override
  void initState() {
    super.initState();
    signedIn();

    widget.auth.currentUser().then((userId) {
      //comment this setState to show login/registration screen
      setState(() {
        authStatus =
            userId == null ? AuthStatus.notSignedIn : AuthStatus.signedIn;
        print(userId);
      });
    }).catchError((onError) {
      authStatus = AuthStatus.notSignedIn;
    });
  }

  void signedIn() {
    if (widget.completed)
      setState(() {
        authStatus = AuthStatus.signedIn;
      });
  }

  @override
  Widget build(BuildContext context) {
    switch (authStatus) {
      case AuthStatus.notSignedIn:
        return RegistrationStartScreen(auth: widget.auth);

      case AuthStatus.signedIn:
        return StartScreen();
    }

    return new RegistrationStartScreen(auth: widget.auth);
  }
}

1 个答案:

答案 0 :(得分:1)

我能够按如下方式发布它。要知道,如果authStatus仍在加载,则必须允许使用空值,因此请将AuthStatus authStatus = AuthStatus.notSignedIn;更改为AuthStatus authStatus

如果值为null,则将一个空白的Container作为备用:

 Widget build(BuildContext context) {
    if (authStatus != null) {
      switch (authStatus) {
        case AuthStatus.notSignedIn:
          return RegistrationStartScreen(auth: widget.auth);

        case AuthStatus.signedIn:
          return StartScreen();
      }

      return RegistrationStartScreen(auth: widget.auth);
    } else {
      return Container();
    }
  }