当我实现启动画面时,应用程序崩溃了

when I implement splash screen, application get crashed

我尝试了两种不同的方法来实现启动画面。 1) 通过更改 launch_background.xml 中的代码,效果很好,但它只会破坏图像。喜欢它显示小图像到非常大的尺寸。当我应用我真正的闪屏图像应用程序时,应用程序崩溃了,但应用程序可以很好地处理其他图像,但每个图像都有图像大小问题。所以我需要知道如何将图像的大小设置到这个文件中以及如何避免应用程序崩溃 2) 我使用的启动画面包也有问题,这个图像尺寸在屏幕中央非常非常小,我想把它作为完整的背景。

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

这是第三题的代码

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'),

    );
  }
}

嘿开发者, 试试这个方法,不需要使用任何外部包...

  @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');
  }
}```


我遇到了类似的问题,只是更喜欢制作自定义启动画面并根据需要放置任何类型的功能。 这是我尝试过的示例,对您来说效果很好。当您需要从主屏幕返回时,请确保以这种方式使用 Navigator.pushReplacement 它不会停留在 spalsh 上,它会退出,因为它应该是合乎逻辑的。根据需要导入包。

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,),
            ),

          ],
        ),
      );
  }
}