Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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? Thanks! P.S. An is there a difference in the process for android and ios?

like image 842
Roman Klym Avatar asked Jan 13 '20 09:01

Roman Klym


People also ask

How do you make gradient color in flutter?

Note: In order to implement any type of gradient we need to make use of Box Decoration. The Box Decoration constructor can be assigned to the decoration property of Containers. To add gradients to our widget we will be using the gradient property of the BoxDecoration constructor.


Video Answer


3 Answers

1 In \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

<?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

<?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>
like image 79
mandreshope Avatar answered Oct 27 '22 09:10

mandreshope


For Android first define two color in your 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:

<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

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.

like image 31
Mehrdad Davoudi Avatar answered Oct 27 '22 09:10

Mehrdad Davoudi


I found use of flutter_native_splash much more easy and direct. 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.

Something like:

    flutter_native_splash: 
         image: ""
         background_image: "assets/my_splash_background.png"
like image 42
Jaybiker Avatar answered Oct 27 '22 11:10

Jaybiker