A well-crafted user interface can transform an app’s experience, making it not only functional but also visually appealing. One elegant effect that enhances design is a linear gradient border. In this blog, we’ll explore how to achieve this effortlessly using react-native-linear-gradient
.
🚀 Prerequisites
Before we dive in, ensure your project has react-native-linear-gradient
installed:
npm install react-native-linear-gradient
---or----
yarn add react-native-linear-gradient
For Expo users:
expo install expo-linear-gradient
Once installed, we’re ready to bring some gradient magic to our UI! 🎨
🎨 Building a Gradient Border Button
Let's create a button wrapped in a gorgeous gradient border:
import React from "react";
import { View, Text, TouchableOpacity, StyleSheet } from "react-native";
import LinearGradient from "react-native-linear-gradient";
const CustomLinearView = () => {
return (
<View style={styles.container}>
<LinearGradient
colors={["#FF764A", "#F6726D"]}
start={{ x: 0.0, y: 1.0 }}
end={{ x: 1.0, y: 1.0 }}
style={styles.gradientBorder}
>
<TouchableOpacity style={styles.button} activeOpacity={0.7}>
<Text style={styles.text}>Random Text</Text>
</TouchableOpacity>
</LinearGradient>
</View>
);
};
export default BettySuggestionView;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "transparent",
},
gradientBorder: {
height: 44,
justifyContent: "center",
marginRight: 12,
borderRadius: 30,
overflow: "hidden",
alignSelf: "center",
backgroundColor: "transparent",
marginTop: 15,
},
button: {
flex: 1,
alignSelf: "center",
justifyContent: "center",
flexDirection: "row",
backgroundColor: "#000",
paddingHorizontal: 16,
margin: 1,
borderRadius: 30,
overflow: "hidden",
},
text: {
color: "#fff",
fontSize: 16,
fontWeight: "600",
},
});
✍️ Breaking It Down
✔ Gradient Border Effect: We wrap the button inside a LinearGradient
container to create a border illusion.
✔ Padding for Border Thickness: The padding
in gradientBorder
determines the border thickness.
✔ Inner Button Customization: The actual button sits within, maintaining a solid background color.
🎭 Customization Tips
- 🎨 Play with different gradient colors to match your app’s theme.
- 🔄 Modify borderRadius to create a rounder or sharper look.
- 🖌 Adjust padding to increase or decrease border thickness.
This approach not only enhances aesthetics but also keeps the UI smooth and seamless. Try it out and take your app's design to the next level! 🚀
💡 Conclusion
Adding a linear gradient border in React Native is a simple yet effective way to elevate UI design. With just a few lines of code, you can create visually striking buttons and elements. Start experimenting today and let your creativity shine! ✨
Top comments (0)