EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

React Native Linear Gradient

By Rahul DubeyRahul Dubey

Home » Software Development » Software Development Tutorials » React Native Tutorial » React Native Linear Gradient

React Native Linear Gradient

Introduction of React Native Linear Gradient

Linear Gradient is a way for creating an image which has a transition progressing between different colours. Linear Gradient can be used to fill lines, rectangles, texts, circles, images etc. Linear Gradient is not supported by React Native directly but a library named as react native linear gradient helps us in creating a linear gradient for our apps. React native has different components and ways to implement a linear gradient. Linear gradient is easy to make in react native without the need of extra dependencies. In this article, we will go through some examples to understand the application Linear Gradient in React Native.

Syntax of React Native Linear Gradient

Syntax of using React Native Linear Gradient:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. Basic Linear Gradient

<LinearGradient
colors={[' #ed83f2', ' #dafc74', ' #ed4a7b']}
style={styles.linearGradient}>
<Text style={styles.buttonText}>
Basic Linear Gradient Syntax
</Text>
</LinearGradient>

2. Horizontal Linear Gradient

<LinearGradient
start={{x: 1, y: 0}}
end={{x: 0, y: 0}}
colors={['#ed83f2', '#dafc74', '#ed4a7b'']}
style={styles.linearGradient}>
<Text style={styles.buttonText}>
Horizontal Linear Gradient Syntax
</Text>
</LinearGradient>

3. Location Linear Gradient

<LinearGradient
start={{x: 0.0, y: 0.35}}
end={{x: 0.6, y: 2.0}}
locations={[0,0.5,0.6]}
colors={['#ed83f2', '#dafc74', '#ed4a7b'']}
style={styles.linearGradient}>
<Text style={styles.buttonText}>
Location Linear Gradient Syntax
</Text>
</LinearGradient>

Working of React Native Linear Gradient

Linear Gradient can be created in our react native apps using the props described below:

  • colors:  This defines an array of multiple colours which displays the gradient. Example: [‘Yellow’, ‘cyan’] displays the gradient from yellow to cyan.
  • start: This object defines the starting coordinates of the gradient in the following way  : {x:number,y:number}. The coordinates sets the position where the gradient will start. It is written as a fraction of the complete gradient’s size and starts from the top left corner. Example:{x:0.2,y:0.2} defines the gradient to start at 20% from the top and 20% from left corner.
  • end: It works just like start, the only difference is that it defines the gradient’s end.
  • locations: This defines the location where each gradient color will change or stop. It maps to the colors synced with the colors prop. Example: [0.2,0.5,1] defines the first color to start from 0% to 20% then the second color starts from 20% and ends at 50% and now the last color will take the rest of the gradient left.These props will be enough to start with Linear Gradient.

Examples of React Native Linear Gradient

Following are the examples are given below:

Example #1

React Native Linear Gradient Example with Range Slider & Percentage window:

In the example below, one change the colour using the range slider which moves in the range from 0 to 360 or also by editing the range in the percentage box below the range slider. The linear horizontal gradients are already been set up within the code. One changes the gradients by editing the below code according to one’s need.

The files used to implement the code below are:

React Native Linear Gradient-1.1

1. index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#050505">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>React Native Linear Gradient App</title>
</head>
<body>
<noscript>
To run application, enable JavaScript.
</noscript>
<div id="root"></div>
</body>
</html>

2. index.js

import React from "react";
import ReactDOM from "react-dom";
import styled
, { createGlobalStyle } from "styled-components";
const Reset = createGlobalStyle`
* {
margin: 0;
padding: 0;
}
`;
constFullScreen = styled.div`
height: 101vh;
width: 101vw;
`;
const Gradient = styled.div`
background: linear-gradient(
30deg,
hsl(${props =>props.hue}, 120%, 50%),
hsl(${props =>props.hue - 305}, 120%, 0%)
);
height: 105%;
width: 99%;
`;
constCentered = styled.div`
position: absolute;
top: 45%;
left: 45%;
transform: translateX(-45%) translateY(-45%);
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
`;
constRangeInput = styled.input.attrs({
type: "range"
})``;
const RangeInput1 = styled.input.attrs({
type: "percentage"
})``;
const Text = styled.h1`
color: #fcfcfc;
font-family: times;
margin-bottom: 18px;
`;
function App() {
const [hue, setHue] = React.useState(340);
return (
<>
<Reset />
<FullScreen>
<Centered>
<Text>{'\u2740'} React Native Linear Gradient {'\u2740'}</Text>
<RangeInput
value={hue}
onChange={evt =>setHue(evt.target.value)}
min="0"
max="360"
/>
<RangeInput1
value={hue}
onChange={evt =>setHue(evt.target.value)}
min="0"
max="360"
/>
</Centered>
<Gradient hue={hue} />
</FullScreen>
</>
);
}
constrootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:

React Native Linear Gradient-1.2

React Native Linear Gradient-1.3

Example #2

React Native Linear Gradient Example with an Image

In the example below, we have used an image and dummy button. The linear gradients along x-axis (i.e. horizontally) are set up using 3 different colours. One change the gradients by editing the below code according to one’s need.

The files used to implement the code below are:

Output-2.1

1. index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#050505">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>React Native Application</title>
<style>
html,
body {
height: 100%;
}
body {
overflow: hidden;
}
#root {
display: flex;
height: 100%;
}
</style>
</head>
<body>
<noscript>
To run this application, enable JavaScript.
</noscript>
<div id="root"></div>
</body>
</html>

2. App.js

import React
, { Component } from "react";
import { Button
, StyleSheet
, View
, Text } from "react-native";
import FullWidthImage from "react-native-fullwidth-image";
import LinearGradient from "react-native-web-linear-gradient";
import "typeface-roboto";
constlogoUri =
"https://images.pexels.com/photos/3861964/pexels-photo-3861964.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500";
class App extends Component {
render() {
return (
<View style={styles.heroContainer}>
<LinearGradient
colors={["#c4ed5c", "#bb4fdb", "#80161e"]}
style={styles.linearGradient}
/>
<FullWidthImage
accessibilityLabel="Rahul Dubey"
source={{ uri: logoUri }}
/>
<View style={styles.heroHeaderContainer}>
<View style={styles.heroHeaderTextContainer}>
<Text style={styles.heroHeaderText}>React Native.</Text>
<Text style={styles.heroHeaderText}>Linear Gradient.</Text>
</View>
<Button title="Coding is Love" />
</View>
</View>
);
}
}
const styles = StyleSheet.create({
heroContainer: {
position: "relative"
},
linearGradient: {
position: "absolute",
zIndex: 10,
left: 0,
top: 0,
right: 0,
bottom: 0,
opacity: 0.55
},
heroHeaderContainer: {
left: 0,
top: 0,
right: 0,
bottom: "30%",
position: "absolute",
alignItems: "start",
justifyContent: "center",
zIndex: 11,
marginLeft: 45
},
heroHeaderText: {
fontSize: 49,
color: "#fcfcfc",
textTransform: "uppercase"
},
heroHeaderTextContainer: {
marginBottom: 28
}
});
export default App;

3. index.js

import { AppRegistry } from "react-native";
import App from "./App";
AppRegistry.registerComponent("App", () => App);
AppRegistry.runApplication("App", {
rootTag: document.getElementById("root")
});

Output:

Output-2.2

Conclusion

On the basis of the above article, we understood the concept of Linear Gradient and how it is used in React Native. We went through different examples to understand its application in React Native and how it can be used according to our requirements. We hope this article would have made the concept of Linear Gradient easy to understand.

Recommend ed Articles

This is a guide to React Native Linear Gradient. Here we also discuss the introduction and working of react native linear gradient along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. React Native Swift
  2. Styling in React Native
  3. React Native Debugger
  4. React Native Authentication

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
React Native Tutorial
  • Basic
    • What is React Native?
    • React Versions
    • React Constructor
    • React Native Architecture
    • React Native Libraries
    • React Components Libraries
    • React Native Components
    • React Component Library
    • React Component Lifecycle
    • React Native Framework
    • React Higher Order Component
    • React Ternary Operator
    • React Native Charts
    • React Native Layout
    • React Native Grid
    • React Native Fetch
    • React Native Modal
    • React Native SVG
    • Button in React Native
    • React List Components
    • React Native Element
    • React Native FlatList
    • React Native SectionList
    • react native dropdown
    • React Native Menu
    • React Native State
    • React State Management
    • React Native Tabs
    • React Native Tab Bar
    • React Format
    • React-Native Switch
    • React Native Firebase
    • React Native Flexbox
    • React Native StatusBar
    • React Native ScrollView
    • React Native ListView
    • React Native TextInput
    • React Native Table
    • React-Native Border Style
    • React Native Search Bar
    • React-Native StyleSheet
    • React Native Vector Icons
    • React Native Login Screen
    • React Native Splash Screen
    • React Native Image Picker
    • React Native Navigation
    • React Native Swift
    • React Controlled Input
    • React Fragment
    • React Native Color
    • React Portals
    • React Refs
    • React shouldComponentUpdate()
    • React ComponentDidMount()
    • React componentWillUpdate()
    • React Native Orientation
    • React Native Animation
    • React Native Form
    • React Props
    • React Native Linear Gradient
    • React Native AsyncStorage
    • React Error Boundaries
    • React Native Progress Bar
    • React-Native Calendar
    • React Native Linking
    • React Native DatePicker
    • React Native Image
    • React Native Drawer
    • React Native Drawer Navigation
    • React Native Fonts
    • React Native Overlay
    • React Native OneSignal
    • React Native Template
    • React Native Router
    • React Router Transition
    • React Dispatcher
    • React Native Redux
    • React JSX
    • React native keyboardavoidingview
    • React Native Permissions
    • React Redux Connect
    • React Native Material
    • React Native Gesture Handler
    • React Native Theme
    • React Native Accessibility
    • React Native Justify Content
    • MobX React Native
    • React Native Authentication
    • React Native UUID
    • React Native Geolocation
    • React Native Debugger
    • React Native Carousel
    • React Native Local Storage
    • React Native TypeScript
    • React Bootstrap
    • React Native SQLite
    • React Native Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More