Search code examples
androidfluttersharedpreferencessplash-screen

Flutter - Need help on a proper way to use splash screen + Login data with SharedPreferences checking


I am trying to display a Splash Screen with 3 seconds Timer while getting isLoggedIn value from SharedPreferences. The splash screen that I am using is just a Spinner.

The first time the app is installed, it went smoothly with splash screen running for 3 seconds, navigated into LoginPage and I input login credentials. Then the app straight navigated to the next page called /main which has 3 pages inside it (BottomNavigationBar) with /home as its default index.

Problem is: the next time I launch the app without re-installing it, it does not show any splash screen. It straight went into /main page. After that I instantly tried to move to another page by BottomNavigationBar. And then after 3 seconds (which I am sure is because of Timer), the screen automatically reinitiated and moved back into /home.

Here is my splash_screen_page.dart file:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SplashScreenPage extends StatefulWidget {
  const SplashScreenPage({Key? key}) : super(key: key);

  @override
  State<SplashScreenPage> createState() => _SplashScreenPageState();
}

class _SplashScreenPageState extends State<SplashScreenPage> {
  bool isLoggedIn = false;

  @override
  void initState() {
    super.initState();
    Timer(Duration(seconds: 3), () {
      _navigateUser();

    });
  }

  void _navigateUser() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    isLoggedIn = prefs.getBool('isLoggedIn') ?? false;
    if(isLoggedIn){
      Navigator.pushReplacementNamed(context, "/main");
    }else{
      Navigator.pushReplacementNamed(context, "/login");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Padding(
            padding: EdgeInsets.all(128),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: [
                Container(
                    child:
                        // Image(image: AssetImage("assets/splashscreen.png")),
                        const SpinKitFoldingCube(
                  color: Colors.blue,
                  size: 50.0,
                ))
              ],
            )
        )
    );
  }
}

Solution

  • May be it can help

    class _SplashScreenPageState extends State<SplashScreenPage> {
        bool isLoggedIn = false;
        
        
        @override
        void initState() {
        super.initState();
        
        login().then((value) {
            if(isLoggedIn){
              Navigator.pushReplacementNamed(context, "/main");
            }else{
              Navigator.pushReplacementNamed(context, "/login");
            }
        });
        
        login(){
            int _jobsRemained = 2;
            while (_jobsRemained > 0) {
                if (!isLoggedIn){
                    SharedPreferences prefs = await SharedPreferences.getInstance();
                    isLoggedIn = prefs.getBool('isLoggedIn') ?? false;
                    _jobsRemained--;
                }
                
                Future.delayed(Duration(seconds: 3)).then((value) => {_jobsRemained--;});
            }
            
        }
      }