Home > front end >  flutter splash screen didn't show
flutter splash screen didn't show

Time:12-27

i have a book for flutter beginner programmer. At the first section it show me how to create splash screen, but the problem is that splash screen is doesn't show, just a blank black screen and after that the apps is showing.

This is my splash_screen.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wisata_yogya/main.dart';

void main() =>runApp(SplashScreen());

// ignore: use_key_in_widget_constructors
class SplashScreen extends StatelessWidget{
  @override
  Widget build(BuildContext context){
    return MaterialApp(
      home: _SplashScreenBody(),
    );
  }
}

class _SplashScreenBody extends StatefulWidget{
  @override
  State<StatefulWidget> createState(){
    return _SplashScreenBodyState();
  }
}

class _SplashScreenBodyState extends State<_SplashScreenBody>{
  @override
  Widget build(BuildContext context){
    Future.delayed(const Duration(seconds: 3), (){
      Navigator.pushAndRemoveUntil(context, 
      // ignore: prefer_const_constructors
      MaterialPageRoute(builder: (context) => MyApp()), (Route route) => false);
    });

    return const Scaffold(
      body: Center(
        child: Image(
          image: AssetImage("graphics/logo.png"),
          height: 75,
          width: 75,
        )
      )
    );
  }
}

And there's no error in the code.

CodePudding user response:

Try to add timer in initState instead of what you doing something like this ‘void initState() {
super.initState();
Timer(Duration(seconds: 5),
()=>Navigator.pushReplacement(context,
MaterialPageRoute(builder:
(context) => HomeScreen()
)
)
);
}

CodePudding user response:

Method 1
You can use animated_splash_screen its easy to use,

home: AnimatedSplashScreen(
    splash: Scaffold(
      body: Center(
        child: Image.asset(
          'assets/logo.png',
          width: double.infinity,
          height: double.infinity,
        ),
      ),
    ),
    duration: 100,
    nextScreen: Login(),
    splashTransition: SplashTransition.fadeTransition,
    backgroundColor: Colors.white,

Method 2

Or Use Timer for delay some seconds in the initState()

Timer(
      Duration(seconds: 2),
      () => Navigator.pushReplacement(context,
          MaterialPageRoute(builder: (context) => HomePage())));

CodePudding user response:

You can use this https://pub.dev/packages/flutter_native_splash its easy to use,

CodePudding user response:

class _SplashState extends State {

@override void initState() { super.initState(); Timer(Duration(seconds: 3), ()=>Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) =>First() ) ) ); }

return Scaffold( body: SizedBox.expand( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Container( child: Image.asset('Assets/Group.png'), padding: const EdgeInsets.all(8.0), height: 200, width: 200, ), CircularProgressIndicator(color: HexColor('#22178F'),), ], ), ), );

  • Related