I am trying to create a card that looks like this (https://codepen.io/hemvi/pen/RwqgMew) in react native:
Here’s my attempt to recreate the same in react native:
import {BlurView} from '@react-native-community/blur';
import React from 'react';
import {View, Text, StyleSheet} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
const sytles = StyleSheet.create({
cardContainer: {
color: 'white',
backgroundColor: 'black',
position: 'relative',
borderRadius: 20,
padding: 5,
},
cardContent: {
color: 'white',
padding: 10,
},
gradientStyle: {
position: 'absolute',
top: -5,
left: -5,
right: -5,
bottom: -5,
zIndex: -1,
},
});
export function MyCard() {
return (
<View style={sytles.cardContainer}>
<View style={{borderRadius: 20, backgroundColor: 'black'}}>
<Text style={sytles.cardContent}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
feugiat facilisis ante, eu cursus massa facilisis sit amet. Sed
ullamcorper massa vitae est volutpat, in dapibus elit suscipit. Mauris
ex augue, accumsan ut dui in, finibus varius enim. Quisque ullamcorper
odio at tincidunt scelt dolor vel mauris congue vestibulum. Nulla
accumsan urna elementum ullamcorper ornare. Aenean eu rhoncus justo,
sed viverra diam. Donec ut posuere mi, non fermentum nisl. In at dui
vel erat semper porta. Donec mollis ultricies metus et tempus.
</Text>
</View>
<View style={sytles.gradientStyle}>
<LinearGradient
style={{position: 'absolute', top: 0, left: 0, right: 0, bottom: 0}}
useAngle={true}
angle={90}
locations={[0, 0.5, 1]}
colors={['rgba(0,188,212,1)', 'rgba(6,12,33,1)', 'rgba(0,188,212,1)']}
/>
<BlurView
overlayColor="transparent"
blurType="xlight"
blurAmount={20}
blurRadius={15}
/>
</View>
</View>
);
}
The blur doesn’t seem to apply properly to the linear gradient like it does in web CSS, also note that I am only testing this on an Android device and I don’t know how it looks in iOS.
Android Device output
2
Answers
react-native-linear-gradient
and@react-native-community/blur
may not be compatible.One possible solution is to use a library like
react-native-image-filter-kit
, which provides various image filters and effects, including blur. You can use the BlurFilter component to apply a blur effect to an image or another component.To use
react-native-image-filter-kit
:Then:
I can see two potential solutions.
Extra note:
It has been a while since I wrote React Native but if I remember correctly, the order of the components actually makes a difference behind the scenes for what is rendered in the virtual dom.
You can try moving the Blur View component to before the LinearGradient component in your code to see if that changes anything.
Example:
Finally, I noticed in your code that you misspelled "styles". Even though it doesn’t actually make a difference in this case, make sure you are more careful in the future. I have had thousands of lines of code break because of a typo or misplaced comma.