Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter best way for splash / loading screen

Tags:

flutter

dart

I have a flutter app connected to firebase (firestore).

When the user opens the app, I check if he's a user type 1 or user type 2. + load images from the web etc.

But it takes maybe a second to do all these things. What's the best way to make / implement a loading screen / splash screen that shows up until everything is loaded?

Thanks!

like image 807
Karel Debedts Avatar asked Aug 18 '19 18:08

Karel Debedts


People also ask

How do you make a splash screen Flutter?

To do so, open the Flutter app's Xcode project by typing open ios/Runner. xcworkspace from the root of your app directory. Then select Runner/Assets. xcassets from the Project Navigator and drop in the desired images to the LaunchImage image set.

Is splash screen the same as loading screen?

"Splash screens may be an innocuous part of the user experience." "A splash screen is a screen which appears when you open an app on your mobile device." "Sometimes it's referred to as a launch screen or startup screen and shows up when your app is loading after you've just opened it."


4 Answers

One of the way to do this is using FutureBuilder. The code snippet that I used in my application is given below

FutureBuilder(
    future: _getData(), // the function to get your data from firebase or firestore
    builder : (BuildContext context, AsyncSnapshot snap){
        if(snap.data == null){
            //return loading widget
        }
        else{
            //return the widget that you want to display after loading
        } 
    }
);

The AsyncSnapshot is returned by the function in "future" i.e. in this case _getData().

like image 104
Aashish Shirgave Avatar answered Oct 23 '22 21:10

Aashish Shirgave


You can use Splashscreen plugin, which offers seconds to delay routing to the next page so you can perform your checks, it also has a navigateAfterSeconds which you can write your condition in.

Example:

SplashScreen(
  seconds: 3,
  navigateAfterSeconds: HomePage(),
  title: Text('Splash'),
  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, color: Colors.blueGrey),),
  image: Image.asset('assets/images/icon2.png',),
  backgroundColor: Colors.white,
  styleTextUnderTheLoader: TextStyle(),
  photoSize: 100.0,
  loaderColor: Colors.blue,
  loadingText: Text('initializing...'),
)
like image 37
Mazin Ibrahim Avatar answered Oct 23 '22 22:10

Mazin Ibrahim


My solution is to define a Widget that will display a loading animation and will do some backgroundwork at the same time.

The widget takes a widget that represents the "loading animation" and a function that will be executed in the background.

This is taken from https://github.com/Ephenodrom/EZ-Flutter.

You can find a documentation at https://ez-flutter.de/docs/transition

import 'package:flutter/material.dart';

///
/// Widget for displaying loading animation and doing background work at the same time.
///
class EzTransition extends StatefulWidget {
  EzTransition(this.child, this.toProcess, {this.backgroundColor});

  final Function() toProcess;
  final Widget child;
  final Color backgroundColor;

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

class _EzTransitionState extends State<EzTransition> {
  @override
  void initState() {
    super.initState();
    widget.toProcess();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: getBackgroundColor(),
      child: widget.child,
    );
  }

  Color getBackgroundColor() {
    return widget.backgroundColor == null
        ? Theme.of(context).backgroundColor
        : widget.backgroundColor;
  }
}
like image 42
Ephenodrom Avatar answered Oct 23 '22 23:10

Ephenodrom


I believe the best option, as you don't know exactly how much time it will take is a FutureBuilder which will display a loading animation while you are loading

like image 44
Eyal Kutz Avatar answered Oct 23 '22 23:10

Eyal Kutz