user3532201
user3532201

Reputation: 639

How can I prohibit a short appearance/blink of a RegistrationPage while app is loaded?

I have an app, where I validate on a RootPage at the very beginning if a user is logged in. This is done everytime the app starts.

If he is logged in -> show StartPage If not logged in -> show RegistrationPage

Now when the app loads, the RegistrationPage shortly blinks up (guess you can see it a couple of milliseconds while he is validating the logged in status). I don’t want that.

How can I prohibit the short appearance of the 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);
  }
}

Upvotes: 2

Views: 102

Answers (1)

user3532201
user3532201

Reputation: 639

I was able to post it as follows. To know, if the authStatus is still loading, you have to allow a null value, so change AuthStatus authStatus = AuthStatus.notSignedIn; to AuthStatus authStatus.

Then have a blank Container as a fallback if value is null:

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

Upvotes: 1

Related Questions