你会如何在Flutter应用程序中添加启动画面?它应该在任何其他内容之前加载和显示。目前,在Scaffold(home:X)小部件加载之前,会有一个短暂的颜色闪烁。
当前回答
在flutter中添加启动画面的最简单的方法是imho这个包: https://pub.dev/packages/flutter_native_splash
安装指南(由软件包作者提供):
1. 设置启动画面
将您的设置添加到项目的pubspec中。Yaml文件或在根项目文件夹中创建一个名为flutter_native_splash的文件。Yaml与您的设置。
flutter_native_splash:
image: assets/images/splash.png
color: "42a5f5"
图像必须为PNG文件。
你也可以在颜色中使用#。颜色:“# 42 a5f5” 如果你不想为特定平台创建启动画面,你也可以将android或ios设置为false。
flutter_native_splash:
image: assets/images/splash.png
color: "42a5f5"
android: false
如果你的图像应该使用所有可用的屏幕(宽度和高度),你可以使用填充属性。
flutter_native_splash:
image: assets/images/splash.png
color: "42a5f5"
fill: true
注意:填充属性还没有在iOS启动画面中实现。
如果你想在Android上禁用全屏闪屏,你可以使用android_disable_fullscreen属性。
flutter_native_splash:
image: assets/images/splash.png
color: "42a5f5"
android_disable_fullscreen: true
2. 运行包
添加设置后,运行with的包
Flutter pub运行flutter_native_splash:创建 当包完成运行时,你的启动画面就准备好了。
其他回答
最简单的方法是使用flutter_native_splash包
首先,把它添加到你的开发依赖项中:
dev_dependencies:
flutter_native_splash: ^1.3.1 # make sure to us the latest version
现在,你可以配置你的启动画面你喜欢:
flutter_native_splash:
android: true # show for android, you may set it to false
ios: true # show for IOS, you may set it to false
image: assets\logo.png # the default image for light and dark themes. Until now, images should be png images
image_dark: aassets\logo_dark.png # It will override the 'image' in the dark mode
color: "#ffffff" # the default color for light and dark themes
color_dark: "#0a0a0a" # will override the 'color' in the dark mode
android_gravity: fill # make the image fill the screen for android
ios_content_mode: scaleAspectFill # make the image fill the screen for android
这样做之后,运行:
flutter clean && flutter pub get && flutter pub run flutter_native_splash:create
你会注意到的。“\android\app\src\main\res*”已经改变,并添加了新的启动画面。
为安卓 App -> SRC -> main -> res ->drawble->launch_background.xml 和取消 注释块是这样的
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/launch_image" /></item>
有人在这样编码后会出现错误吗 在android studio中使用与系统同步或使缓存无效并重置。这解决了我的问题 在颤振调试模式需要一些时间采取启动画面。构建后,它将减少像本机android
在flutter中添加启动画面的最简单的方法是imho这个包: https://pub.dev/packages/flutter_native_splash
安装指南(由软件包作者提供):
1. 设置启动画面
将您的设置添加到项目的pubspec中。Yaml文件或在根项目文件夹中创建一个名为flutter_native_splash的文件。Yaml与您的设置。
flutter_native_splash:
image: assets/images/splash.png
color: "42a5f5"
图像必须为PNG文件。
你也可以在颜色中使用#。颜色:“# 42 a5f5” 如果你不想为特定平台创建启动画面,你也可以将android或ios设置为false。
flutter_native_splash:
image: assets/images/splash.png
color: "42a5f5"
android: false
如果你的图像应该使用所有可用的屏幕(宽度和高度),你可以使用填充属性。
flutter_native_splash:
image: assets/images/splash.png
color: "42a5f5"
fill: true
注意:填充属性还没有在iOS启动画面中实现。
如果你想在Android上禁用全屏闪屏,你可以使用android_disable_fullscreen属性。
flutter_native_splash:
image: assets/images/splash.png
color: "42a5f5"
android_disable_fullscreen: true
2. 运行包
添加设置后,运行with的包
Flutter pub运行flutter_native_splash:创建 当包完成运行时,你的启动画面就准备好了。
对于那些像我一样严格按照步骤来做的人来说,它仍然没有发挥作用……
如果你有一个drawable-v21文件夹,请注意,因为你需要将你的代码从drawable文件夹> launch_background.xml code复制到drawable-v21文件夹> launch_background.xml
我的项目不喜欢android:src="@mipmap/launch" />
因此,我有android:src="@drawable/launch" />
让你的材料应用像这样
=>添加依赖
=> import import 'package:splashscreen/splashscreen.dart';
import 'package:flutter/material.dart';
import 'package:splashscreen/splashscreen.dart';
import 'package:tic_tac_toe/HomePage.dart';
void main(){
runApp(
MaterialApp(
darkTheme: ThemeData.dark(),
debugShowCheckedModeBanner: false,
home: new MyApp(),
)
);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return new SplashScreen(
seconds: 6,
navigateAfterSeconds: new HomePage(),
title: new Text('Welcome',
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 26.0,
color: Colors.purple,
),
),
image: Image.asset("images/pic9.png"),
backgroundColor: Colors.white,
photoSize: 150.0,
);
}
}
最后的屏幕输出像这样,你可以根据你的要求改变秒 圆将是圆的