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

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

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

如何禁止RegistrationPage出现太短?

根页

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);
  }
}

最佳答案

我能够将其发布如下。要知道,如果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();
    }
  }

关于dart - 加载应用程序时,如何禁止RegistrationPage出现短暂外观/闪烁?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55448235/

10-12 00:05