简体   繁体   English

有没有办法根据 Apps 主题更改初始屏幕颜色?

[英]Is there a way to change the splash screen color based on the Apps theme?

Can the native android splash screen color be changed programmatically, so that it can be set fitting to the theme chosen for the Flutter app?能否以编程方式更改原生 android 启动画面颜色,以便可以将其设置为适合为 Flutter 应用程序选择的主题?

Kind of like how the WhatsApp splash screen loads in a dark theme when the System theme is set to dark.有点像当系统主题设置为深色时 WhatsApp 启动画面在深色主题中的加载方式。

Dark and light WhatsApp splash screen:深色和浅色 WhatsApp 启动画面:

深色和浅色 WhatsApp 启动画面

I guess this uses two themes for the splash screen, which are linked to the system theme, but I don't know that much about how and if that's possible.我猜这使用两个主题作为启动画面,它们链接到系统主题,但我不太了解如何以及是否可能。

So the question is: Is it possible to change the color/theme of the splash screen while running a flutter application, so that on the next start of the app the native android splash screen loads in the same theme as the Flutter app?所以问题是:是否可以在运行 flutter 应用程序时更改启动画面的颜色/主题,以便在应用程序的下一次启动时,原生 android 启动画面以与 Flutter 应用程序相同的主题加载?

(Sorry if I repeated myself a lot, but I want to describe what I'm asking as good as possible) (对不起,如果我重复了很多,但我想尽可能好地描述我的要求)

In your project folder's android/app/src/main/res there should be a drawable folder, which contains the launch_background.xml for light theme.在您的项目文件夹的android/app/src/main/res中应该有一个drawable文件夹,其中包含用于浅色主题的launch_background.xml Duplicate this folder and call the second one drawable-night and configure the dark theme style.复制此文件夹并调用第二个drawable-night并配置深色主题样式。 It will automatically change based on Android's system theme.它将根据 Android 的系统主题自动更改。

The launch_background.xml in the drawable (light theme) folder can be structured as so, to display an image with a white background: drawable (浅色主题)文件夹中的launch_background.xml可以这样构造,以显示具有白色背景的图像:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/launch_image" />
    </item>
</layer-list>

The same can be done in the launch_background.xml in the drawable-night (dark theme) folder, replacing "@android:color/white" with "@android:color/black" for a black background.同样可以在drawable-night (深色主题)文件夹中的launch_background.xml中完成,将"@android:color/white"替换为"@android:color/black"以获得黑色背景。

Do note that the images must be inside of the drawable and drawable-night folders being used respectively.请注意,图像必须分别位于正在使用的drawabledrawable-night文件夹内。 As far as I know, the format must be a png.据我所知,格式必须是png。

I found this useful article that addresses and explains native splash screen theming.我发现这篇有用的文章解决并解释了原生启动画面主题。

If you set the splash screen from the Android folder xml files, then i guess you can't escape writing some Native code and placing some of your theming logic in the Android subfolder.如果您从 Android 文件夹 xml 文件设置启动画面,那么我想您无法避免编写一些本机代码并将一些主题逻辑放在 Android 子文件夹中。 On the other hand, if the theming logic and splashscreen is both in the main.dart file.另一方面,如果主题逻辑和启动画面都在main.dart文件中。 Then you can just store the two different splashscreens you want to use and add some if-else statement to display the splash screen and also save user theme preference.然后,你可以保存您想使用两个不同的splashscreens,并添加一些if-else语句来显示启动画面,并保存用户主题偏好。 If you don't know how to do this, you can follow this thread: How to load theme at beginning in fluttter如果您不知道如何执行此操作,可以关注此主题: 如何在fluttter 开始时加载主题

You can use some sort of SharedPreferences to hold the current state of the Theme.您可以使用某种SharedPreferences来保存主题的当前状态。 Then return the desired theme to the MaterialApp based on the value saved in your Shared Preferences .然后根据您的Shared Preferences保存的值将所需的主题返回给MaterialApp I personally use HydratedBloc package to save the current states of my app, including Theme.我个人使用HydratedBloc包来保存我的应用程序的当前状态,包括主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM