Shadow does not appear in react native - reactjs

I want to add a shadow to my card component but it does not appear neither ios nor android, what is the problem?
function Card(props) {
return (
<View style={styles.box}>
<View style={styles.inner}>{props.children}</View> {// Takes an image}
</View>
);
}
const styles = StyleSheet.create({
box: {
width: "50%",
height: "50%",
padding: 15,
shadowColor: "#000", {// the shadow styles for android and ios}
shadowOffset: {
width: 0,
height: 6,
},
shadowOpacity: 0.37,
shadowRadius: 7.49,
elevation: 12,
},
inner: {
flex: 1,
backgroundColor: "#eee",
alignItems: "center",
justifyContent: "center",
},
});
Note: It takes an image and the image cover my whole Card component
Also Here is the parent components styles:
container: {
width: "100%",
height: "85%",
padding: 10,
flexDirection: "row",
flexWrap: "wrap",
flex: 1,
alignItems: "center",
justifyContent: "center",
},
imageStyle: {
resizeMode: "stretch",
width: "100%",
height: "100%",
borderRadius: 10,
overflow: "hidden",
},
An example of parent component:
<View style={styles.container}>
<Card>
<Image
style={styles.imageStyle}
source={require("../assets/games/Among-us.png")}
/>
</Card>
</View>

Problem lies in the fact that you're not applying backgroundColor to your styles.box
Update your styles.box to match this:
box: {
width: "50%",
height: "50%",
padding: 15,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 6,
},
shadowOpacity: 0.37,
shadowRadius: 7.49,
elevation: 12,
backgroundColor: '#ffffff'
}
And you'll get to see your shadow.
This is known bug as you can read here Android - elevation style property does not work without backgroundColor

Related

Flex value for main view causing all of my content to dissapear

I am working on a react native coffee app and trying to create the lay out where I have
the header
the scrollable container holding my coffee
a footer with a text input
a '+' icon to add the coffees to the container
Currently the flex value of my main 'View' is causing my entire app to push to the top of the screen and i have no idea why. I can set the height and width to 100% without flex and i can fill the screen but then i cannot configure my footer properly (+ i know this is bad practice..)
What am i doing wrong with my styles?
const Main = () => {
return(
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerText}> List of Coffee </Text>
</View>
<ScrollView styles={styles.scrollContainer}>
</ScrollView>
<View styles={styles.footer}>
<TextInput
styles={styles.textInput}
placeholder="add your latest coffee here"
placeholderTextColor="#EEE">
</TextInput>
</View>
<TouchableOpacity style={styles.addButton}>
<Text style={styles.addButtonText}>+</Text>
</TouchableOpacity>
</View>
);};
const styles = StyleSheet.create({
container: {
flex:1,
flexDirection:"column",
backgroundColor: 'grey',
},
footer: {
position: 'absolute',
bottom: 10,
left: 10,
right: 10,
backgroundColor: 'white',
zIndex: 9,
},
header: {
backgroundColor: 'black',
alignItems: 'center',
justifyContent: 'center',
borderBottomWidth: 10,
borderBottomColor: 'yellow' ,
paddingTop: 20,
},
headerText: {
color: 'white',
fontSize: 36,
padding: 26,
fontWeight: "500",
fontFamily: 'Helvetica',
},
scrollContainer:{
marginBottom: 100,
},
textInput: {
alignSelf: 'stretch',
color: 'black',
padding:20,
backgroundColor: 'black',
borderTopWidth: 2,
borderTopColor: 'black',
fontSize: 32,
},
addButton: {
position: 'absolute',
zIndex: 11,
right: 20,
bottom: 100,
backgroundColor: 'black',
width: 80,
height: 80,
borderRadius: 50,
justifyContent: 'center',
alignItems: 'center',
} ,
addButtonText:{
color: '#FFD700',
fontSize: 36,
fontWeight: '700',
},
});
export default Main;

How to match the borderRadius of a child view with the parent view borderRadius

Does anyone know how to fix the borderRadius issue in the following? The borderTopLeftRadius and borderTopRightRadius are set to 30 as is the borderRadius in the parent view, but there's whitespace, and they aren't matching up.
Here's the code:
<View style={styles.card}>
<View style={styles.cardHeader}>
<Text style={styles.cardHeaderText}>Title</Text>
</View>
</View>
Styling:
const styles = StyleSheet.create({
card: {
flexDirection: 'column',
margin: 10,
width: '95%',
height: 500,
backgroundColor: '#FFF',
borderRadius: 30,
borderWidth: 5,
},
cardHeader: {
width: '100%',
height: '12%',
backgroundColor: 'green',
borderBottomWidth: 5,
justifyContent: 'center',
alignContent: 'center',
borderTopLeftRadius: 30,
borderTopRightRadius: 30,
},
cardHeaderText: {
fontSize: 20,
fontWeight: 'bold',
paddingHorizontal: 16,
},
)}
The radius of the inner View should be equal to the outer radius minus the width of the border. So, 30-5=25.
You can use border radius = 25 as Abe mentioned or
you can remove border radius of cardHeader and add overflow:"hidden" to the card
const styles = StyleSheet.create({
card: {
...all your styles,
overflow: "hidden"
},
cardHeader: {
width: '100%',
height: '12%',
backgroundColor: 'green',
borderBottomWidth: 5,
justifyContent: 'center',
alignContent: 'center',
//borderTopLeftRadius: 30,
//borderTopRightRadius: 30,
},
)}

How to make center of barcode scanner transparent?

I am creating a barcode scanner using expo-barcode-scanner
I have created the below screen the whole screen is transparent
I am trying to create the below screen only the QR code square is transparent
<BarCodeScanner
style={styles.mainContainer}>
<TouchableOpacity
onPress={() => {
navigation.navigate('Home')
}}
style={styles.backButton}
>
<Entypo
style={{ color: Colors.SAWhite }}
name="chevron-thin-left"
size={25}
/>
</TouchableOpacity>
<View style={styles.qrContainer}></View>
<View style={styles.messageBox}>
<FontAwesomeIcon
name="qrcode"
size={50}
style={isValid ? styles.initialColor : styles.errorColor}
/>
{isValid ? (
<Text style={[fonts.SemiBoldTitle, styles.initialColor]}>
{Languages.Scanner.initialMessage}
</Text>
) : (
<Text style={[fonts.SemiBoldTitle, styles.errorColor]}>
{Languages.Scanner.errorMessage}
</Text>
)}
</View>
</BarCodeScanner>
styles
const styles = StyleSheet.create({
mainContainer: {
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
overflow: 'hidden',
},
backButton: {
alignSelf: 'flex-start',
marginLeft: '5%',
},
qrContainer: {
width: 220,
height: 220,
borderColor: Colors.SAWhite, //white
borderWidth: 1,
margin: '10%',
},
messageBox: {
width: '85%',
height: '30%',
backgroundColor: Colors.SAWhite,
borderColor: Colors.SABlack,
borderWidth: 1,
borderRadius: 10,
alignItems: 'center',
justifyContent: 'space-evenly',
},
initialColor: {
color: Colors.SASecondary,//grey
textAlign: 'center',
marginLeft: '10%',
marginRight: '10%',
},
errorColor: {
color: Colors.SARed,
textAlign: 'center',
marginLeft: '10%',
marginRight: '10%',
},
})
I have tried to wrap it around a view but it also makes the center box as coloured
i have tried expo's barcode code but it is also not good implementation
You can build up the overlay around the transparent part using multiple absolute positioned views, leaving the center empty.
const scanOverlay = {
position: 'absolute',
backgroundColor: 'rgba(255,0,0,0.5)',
};
<View>
<Camera />
<View style={StyleSheet.absoluteFill}>
<View style={[scanOverlay, {top: 0, left: 0, width: '25%', bottom: 0}]} />
<View style={[scanOverlay, {top: 0, left: '25%', right: '25%', height: '25%'}]} />
<View style={[scanOverlay, {bottom: 0, left: '25%', right: '25%', height: '25%'}]} />
<View style={[scanOverlay, {top: 0, right: 0, width: '25%', bottom: 0}]} />
</View>
</View>;

React-native sticking the image to the bottom

I want to make the image sticking to the bottom of the app. How can I do it ? I have tried different stack overflow solutions but I still cannot get the result. As you can see there is a large white space between the red border and the image. The image is trim and there should be no extra space under it. Thanks in advance !
const TubeBoard = () =>
(
<View style={styles.container}>
<Image
source={bigTube}
style={styles.imageStyle}
resizeMode="contain"
/>
</View>
);
const styles = StyleSheet.create({
container: {
justifyContent: 'flex-end',
width: deviceWidth,
height: deviceHeight,
},
imageStyle: {
borderColor: 'red',
borderWidth: 10,
position: 'absolute',
bottom: 0,
width: '100%',
},
});
screenshot here
Solved : Get the desired result by adding { flex: 1 } to container, and height to the image.
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
alignItems: 'flex-start',
justifyContent: 'flex-start',
borderColor: 'blue',
borderWidth: 10,
},
imageStyle: {
borderColor: 'red',
borderWidth: 10,
position: 'absolute',
bottom: 0,
width: deviceWidth,
height: deviceHeight * 0.75,
},
});
desired result
I believe that your container view is not filling the entire screen.
Try adding flex:1 to your container style.
const styles = StyleSheet.create({
container: {
...
},
imageStyle: {
borderColor: 'red',
borderWidth: 10,
width: deviceWidth,
height: 100
},
});
Get the desired result by adding { flex: 1 } to container, and height to the image.
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
alignItems: 'flex-start',
justifyContent: 'flex-start',
borderColor: 'blue',
borderWidth: 10,
},
imageStyle: {
borderColor: 'red',
borderWidth: 10,
position: 'absolute',
bottom: 0,
width: deviceWidth,
height: deviceHeight * 0.75,
},
});

styling of background color in pixel phone

I want to put the logo of the company so that it stretches out from top left corner to the top right corner with width of 10. Below is my code. My image is not showing properly. It shows in the middle of the screen with width going outside of the phone.If I put position: absolute then the image disappears from the phone.
/**
* Sample React Native App
* https://github.com/facebook/react-native
* #flow
*/
import React, { Component } from 'react';
import { Text, View, StyleSheet, Image, ScrollView, TouchableOpacity, Linking, Button } from 'react-native';
import { connect } from 'react-redux';
import { getTheme } from 'react-native-material-kit';
import EvilIcon from 'react-native-vector-icons/EvilIcons';
import MaterialIcon from 'react-native-vector-icons/MaterialIcons';
import SimpleIcon from 'react-native-vector-icons/SimpleLineIcons';
import * as actions from '../actions';
import getDirections from 'react-native-google-maps-directions'
const theme = getTheme();
const styles = StyleSheet.create({
card: {
marginTop: 10,
paddingBottom: 20,
marginBottom: 20,
borderColor: 'lightgrey',
borderWidth: 0.5,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#4F6D7A',
height: 550,
alignSelf:'center',
width:500,
position: 'relative',
},
title1: {
top: 10,
left: 80,
fontSize: 24,
},
title2: {
top: 35,
left: 82,
fontSize: 18,
},
image: {
flex: 0,
height: 100,
width: 333,
backgroundColor: 'transparent',
justifyContent: 'center',
alignItems: 'center',
},
closeIcon: {
position: 'absolute',
top: 5,
left: 295,
color: 'rgba(233,166,154,0.8)',
backgroundColor: 'rgba(255,255,255,0)',
},
icon: {
position: 'absolute',
top: 15,
left: 0,
color: 'white',
backgroundColor: 'rgba(255,255,255,0)',
},
textArea: {
flexDirection: 'row',
paddingLeft: 20,
paddingTop: 10,
width: 260,
},
textIcons: {
color: '#26a69a',
},
actionArea: {
paddingTop: 10,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
},
title:{
justifyContent: 'center',
paddingTop: 10,
alignItems: 'center',
alignSelf: 'center',
fontWeight: 'bold',
fontSize: 22,
color: '#F5FCFF',
},
SerContent:{
fontWeight: 'bold',
fontSize: 16,
paddingTop: 10,
alignSelf: 'center',
color: '#F5FCFF'
},
underLineText: {
fontSize: 17,
textDecorationLine: 'underline',
color: '#F5FCFF',
fontWeight: 'bold',
textAlign: 'center',
},
dir:{
flexDirection:'row',
paddingTop: 30,
textAlign: 'center',
fontSize: 17,
alignSelf: 'center'
} ,
Address1:{
alignSelf: 'center',
marginRight: 20,
fontSize: 17,
fontWeight: 'bold',
color: '#F5FCFF',
},
toolbar: {
flexDirection: 'row', //Step 1
},
toolbarTitle:{
width: 10,
top: 0,
left: 0,
bottom: 0,
right: 0,
flex:1 ,
position:'absolute' //Step 3
},
});
class ServiceDetail extends Component {
handleClick = (link) => {
Linking.canOpenURL(link).then(suppported => {
if (supported) {
Linking.openURL(link);
} else {
console.log('Don\'t know how to open URI: ' + link);
}
});
};
render() {
var destUrl = 'https://www.google.com/maps/search/?api=1&query=' + this.props.services.destAddr1 + '+' + 'field'
var destUrl1 = 'https://www.google.com/maps/search/?api=1&query=' + this.props.services.destAddr2 + '+' + 'field'
return (
<ScrollView showsVerticalScrollIndicator={false}>
<View style={styles.container}>
<View style={styles.toolbar}>
<Image
resizeMode='contain'
style={styles.toolbarTitle}
source={require('../Resources/LogoWithDesc.jpg')} />
</View>
<SimpleIcon name={'close'} size={30} style={styles.closeIcon}
onPress={() => this.props.noneSelected()} />
<Text style={styles.title}>{this.props.services.ser}</Text>
<Text style={styles.SerContent} >Service is available in the following locations:</Text>
<View style={styles.dir}>
<Text style={styles.Address1}> {this.props.services.Location}:</Text>
<TouchableOpacity onPress={() => Linking.openURL(destUrl)}>
<Text style={styles.underLineText}>Directions</Text>
</TouchableOpacity>
</View>
<View style={styles.dir}>
<Text style={styles.Address1}>{this.props.services.SecondLoc}:</Text>
<TouchableOpacity onPress={() => Linking.openURL(destUrl1)}></TouchableOpacity>
<TouchableOpacity onPress={() => Linking.openURL(destUrl1)}>
<Text style={styles.underLineText}>Directions</Text>
</TouchableOpacity>
</View>
</View>
</ScrollView>
);
}
}
const mapStateToProps = state => {
return {
services: state.serviceSelected
};
};
export default connect(mapStateToProps, actions)(ServiceDetail);
any help will be highly appreciated.
Please try increasing flex to Image and add the position property.
image: {
flex: 1,
height: 200,
backgroundColor: 'transparent',
justifyContent: 'center',
alignItems: 'center',
width: 333,
position: 'absolute',
},
Please apply the width of the container and add the position property.
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#4F6D7A',
height: 200,
alignSelf:'center'
width: 333,
position: 'relative',
},
you can also edit the layout using flexbox
https://facebook.github.io/react-native/docs/flexbox.html

Resources