简体   繁体   English

如何在 Flutter 中为启动画面创建渐变色背景?

[英]How to create a gradient color background for splash screen in Flutter?

I'm working on the splash screen for my Flutter app, and in drawable folder I have to create a file called colors.xml in order to change background color for my splash screen.我正在为我的 Flutter 应用程序处理启动画面,在可绘制文件夹中,我必须创建一个名为 colors.xml 的文件,以便更改启动画面的背景颜色。 I'm finding it hard to make it a gradient color.我发现很难使它成为渐变色。 My intention is to create a gradient background color that would start at the top left and end at bottom right, using two different colors. Does anyone have an example of how to do that in Flutter?我的意图是使用两个不同的 colors 创建一个从左上角开始到右下角结束的渐变背景颜色。有没有人在 Flutter 中有一个如何做到这一点的例子? Thanks.谢谢。 PS?附言? An is there a difference in the process for android and ios? android 和 ios 的流程有区别吗?

1 In \\android\\app\\src\\main\\res\\drawable\\launch_background.xml 1 在\\android\\app\\src\\main\\res\\drawable\\launch_background.xml

change this :改变这个:

<item android:drawable="@android:color/white" />

to :到 :

<item android:drawable="@drawable/gradient_background" />

2 Create this file \\android\\app\\src\\main\\res\\values\\colors.xml 2 创建这个文件\\android\\app\\src\\main\\res\\values\\colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="gradientstart">#3587d0</color>
    <color name="gradientend">#36f1d3</color>
</resources>

3 Finally, create this file \\android\\app\\src\\main\\res\\drawable\\gradient_background.xml 3 最后,创建这个文件\\android\\app\\src\\main\\res\\drawable\\gradient_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="@color/gradientstart"
        android:endColor="@color/gradientend"
        android:angle="90"/>    
</shape>

For Android first define two color in your colors.xml:对于 Android,首先在你的 colors.xml 中定义两种颜色:

<color name="gradientstart">#888888</color>
<color name="gradientend">#111111</color>

then in \\android\\app\\src\\main\\res\\drawable\\launch_background.xml just replace this:然后在 \\android\\app\\src\\main\\res\\drawable\\launch_background.xml 中替换这个:

<item android:drawable="@color/background" />

to this:对此:

<gradient android:startColor="@color/gradientstart" android:endColor="@color/gradientend" android:angle="315"/>

and for ios according to this question和根据这个问题的ios

If your gradient is a simple vertical or horizontal gradient, and you're really concerned about asset size, you can define a very narrow image and then add an image view with “scale to fill” content mode.如果你的渐变是一个简单的垂直或水平渐变,并且你真的很关心资产的大小,你可以定义一个非常窄的图像,然后添加一个具有“缩放以填充”内容模式的图像视图。

But these images are so small anyway, the amount of space saved will be negligible, so I'm not sure it's worth worrying about.但是这些图像无论如何都太小了,节省的空间量可以忽略不计,所以我不确定是否值得担心。

I found use of flutter_native_splash much more easy and direct.我发现使用 flutter_native_splash 更加简单和直接。 Here's a link on the steps to creating one.这是创建步骤的链接 First design your desired gradient as an image and instead of adding color, add a background_image on the pubspec.yaml file.首先将所需的渐变设计为图像,而不是添加颜色,而是在 pubspec.yaml 文件中添加背景图像。

Something like:就像是:

    flutter_native_splash: 
         image: ""
         background_image: "assets/my_splash_background.png"

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

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