Developer
Developer

Reputation: 789

when I implement splash screen, application get crashed

I have tried 2 different ways to implement splashscreen. 1) By changing code in launch_background.xml that works well but it just destroy images. like it show small image to very large size. and when I apply my real splash screen image application get crashed but application is working well with other images but image size problem in every image. so I need to know how to set the size of the image into this file and how to avoid to crash the application 2) I use splash screen package that also have a problem in this image size is very very small in the center of the screen I want to have it as full background.

<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/splash"
        />
</item>

This is the code for 3rd problem

class splash extends StatefulWidget {
  @override
  _splashState createState() =>  _splashState();
}

class _splashState extends State<splash> {
  @override
  Widget build(BuildContext context) {
    return  SplashScreen(
        seconds: 4,
        navigateAfterSeconds:  MyApp(),
        image:  Image.asset('imges/bg.png'),

    );
  }
}

Upvotes: 0

Views: 161

Answers (2)

DmDev
DmDev

Reputation: 602

I had similar issue just prefer to make custom splash screen and put any type of functionality as you want. Here is the sample I did try that will work fine for you. Make sure to use Navigator.pushReplacement in such a way when you need to get back from home screen it would not stay on spalsh it will exit as it should be logically. import packages according to your need.

void main() {
  runApp(MaterialApp(
    home: splash(),
  ));
}

class splash extends StatefulWidget {
  @override
  _splashState createState() =>  _splashState();
}

class _splashState extends State<splash> {

  void initState() {
    super.initState();
    Timer(
      Duration(seconds: 2),
          () => Navigator.pushReplacement(
        context,
        MaterialPageRoute(builder: (context) => yournextpagename(),
      ),
    ));
  }

  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
          children: <Widget>[
            SizedBox.expand(
              child: Image.asset('imges/splash.png',fit: BoxFit.fill,),
            ),

          ],
        ),
      );
  }
}

Upvotes: 1

Zeeshan Ansari
Zeeshan Ansari

Reputation: 1483

Hey Developer, Just try this way, no need to use any external packages...

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

class _SplashScreenState extends State<SplashScreenAnimate> {
  startTime() async {
    var _duration = new Duration(seconds: 8);
    return new Timer(_duration, navigationPage);
  }

  Future checkFirstSeen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool _seen = (prefs.getBool('seen') ?? false);

    if (_seen)
      return false;
    else {
      prefs.setBool('seen', true);
      return true;
    }
  }

  @override
  void initState() {
    super.initState();
    startTime();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.white,
      body: new Center(
        child: new Image.asset('assets/images/splash_screen.png',
            height: MediaQuery.of(context).size.height, fit: BoxFit.fill),
      ),
    );
  }

  navigationPage() async {
    bool isFirstSeen = await checkFirstSeen();

    if (isFirstSeen) {
      return Navigator.pushNamed(context, '/onboardscreen');
    }

    if (kAdvanceConfig['IsRequiredLogin']) {
      return Navigator.pushReplacementNamed(context, '/login');
    }

    return Navigator.pushReplacementNamed(context, '/home');
  }
}```


Upvotes: 0

Related Questions