Home > OS >  How to await splash screen while loading a home screen in flutter
How to await splash screen while loading a home screen in flutter

Time:12-24

my homepage is taking sometime to load all asset, so I want to add a splash screen there , but it should wait for 3 second (the time that take load home screen), so I wanna cover the loading section with splash screen

CodePudding user response:

There is package that does exactly that, I use it for real world projet, the setup is easy : https://pub.dev/packages/flutter_native_splash

Do a hard restart and test out, It will wait before your app is loaded and then the splash screen will disappear

CodePudding user response:

You can add a image as a splashscreen using this method,

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

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

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    Timer(const Duration(seconds: 3), () {
      Navigator.of(context)
          .pushReplacement(MaterialPageRoute(builder: (_) => const Home()));
     
    });
  }

  @override

And if you want to increase or decrease the time span then you can change this

Timer(const Duration(seconds: 3).

CodePudding user response:

Use you own custom image of splashscreen or design new one here.increase or decrease time but less time is better

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    startTime();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    Size size = MediaQuery.of(context).size;
    return SafeArea(
      child: Scaffold(
        body: new Stack(
          alignment: Alignment.center,
          children: <Widget>[
            Center(
              child: new Image.asset(
                'assets/images/splash.png',
                width: size.width,
                height: size.height,
                fit: BoxFit.fill,
              ),
            ),
          ],
        ),
      ),
    );
  }
  startTime() async {
    var _duration = new Duration(seconds: 5);
    return new Timer(_duration, navigationPage);
  }
  void navigationPage() {


        Navigator.pushAndRemoveUntil(
            context,
            MaterialPageRoute(builder: (context) => DashBoardScreen()),
            ModalRoute.withName("/login"));
     
  }
}
  • Related