{{announcement.body}}
{{announcement.title}}

How to Create React Native Gradients

DZone 's Guide to

How to Create React Native Gradients

In this article, see how to create React Native gradients.

· Web Dev Zone ·
Free Resource

Gradients bring out a colorful taste in our apps. So it’s important to know how and when to use them in your mobile apps. In React Native, gradients are extremely easy to integrate, and in this article, we are going to describe exactly how to implement them. There are mainly two types of linear gradients in React Native and CSS: Linear and Radial gradient

Two of the most used JavaScript libraries for generating gradients in React Native are

Java
 




x


 
1
expo-linear-gradient and react-native-linear-gradient


They are both very similar and for this article on gradients in React Native we’re going to make use of expo-linear-gradient.

Installation:

For Expo: expo install expo-linear-gradient

For bare projects: 

  • Install and configure unimodules
  • expo install expo-linear-gradient
  • cd ios && pod install

First use case of gradients are to make buttons look solid:


JavaScript


The color prop accepts an array of colors to be displayed in a linear gradient.

Let’s make another nice thing with gradients: We are going to combine an image with a gradient, like the example below:

Now, what’s going on here? We’re overlaying a gradient on an ImageBackground, a React Native component and then reduce the opacity of the gradient. Then we center our the text inside the LinearGradient.

JavaScript
 




xxxxxxxxxx
1
46


 
1
import React from "react";
2
import { StyleSheet, Text, View, ImageBackground } from "react-native";
3
import { LinearGradient } from "expo-linear-gradient";
4
 
          
5
function GradientExampleOne({ navigation }) {
6
  return (
7
    <View style={styles.container}>
8
      <ImageBackground source={require('./assets/workspace.jpg')} style={styles.imgBackground}>
9
        <LinearGradient
10
          colors={["#09203f", "#537895"]}
11
          start={[0.1, 0.1]}
12
          style={styles.linearGradient}
13
        >
14
          <Text style={styles.text} >Linear Gradient Example</Text>
15
        </LinearGradient>
16
      </ImageBackground>
17
    </View>
18
  );
19
}
20
 
          
21
const styles = StyleSheet.create({
22
  container: {
23
    flex: 1,
24
    backgroundColor: "#fff",
25
    alignItems: "center",
26
    justifyContent: "center",
27
  },
28
  imgBackground: {
29
    flex: 1,
30
    width: "100%",
31
    alignItems: "center",
32
  },
33
  linearGradient: {
34
    width: '100%',
35
    height: '100%',
36
    opacity: 0.95,
37
    justifyContent: 'center',
38
    alignItems: 'center'
39
  },
40
  text: {
41
    color: '#fff',
42
    fontSize: 40,
43
    fontWeight: 'bold',
44
    textAlign: 'center'
45
  }
46
});


I introduced a prop start, the start prop is an array to tell the gradient where to start. The first element positions the gradient horizontally(x-axis), while the second element positions the gradient vertically.

For example, [0.1, 0.2] means that the gradient will start 10% from the left and 20% from the top.

Let us see some visual examples:

This is what a gradient with start [0.9, 0.9] looks like.

LinearGradient in React Native is easy and seamless to implement. We have used them to make our buttons have a solid look and feel. We have made a crisp UI background for our text and looked into the start prop.

This is how you can implement gradients in React Native. As you can see, it was pretty straightforward.

Topics:
javascript, linear gradients, react, react native, tutorial, web dev

Published at DZone with permission of Krissanawat Kaewsanmuang . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}