React Native code for Columns with Icons on the side? - react-native

I am trying to write a custom component for this "table" type Figma design doc. Should be an icon on the left that takes .25 of the row and .75 of the row is text.
And I should be able to duplicate that right under it.
Below is the code that I've tried, but the image and text actually overflow the page, and I suspect I'm using too many "flex"'s incorrectly or there might be a much simpler way to do. Any ideas? Thanks!
export function TwoColumnIconLeft (props){
const title = props.title;
const subtitle = props.subtitle;
const body = props.body;
return (
<View
style={{
flex: 1,
flexDirection: "column",
padding: 20
}}>
<View
style={{
flex: .5,
flexDirection: "row",
}}
>
<Image style={{flex: .25}} source={require("./../Assets/watermelon.png")}/>
{/* <Text style={[{ backgroundColor: "red", flex: .25 }, styles.subTitle]}>{subtitle}</Text> */}
<Text style={[{ backgroundColor: "blue", flex: .75}, styles.title]}>{title}</Text>
</View>
<View
style={{
flex: .5,
flexDirection: "row",
}}
>
<Text style={[{ backgroundColor: "red", flex: .25 }, styles.subTitle]}>{subtitle}</Text>
<Text style={[{ backgroundColor: "blue", flex: .75}, styles.title]}>{title}</Text>
</View>
</View>
);
};[enter image description here][1]

This should do the trick for you:
<View style={{flex:1, flexDirection: 'row'}}>
<View style={{flex: .25, flexDirection: 'column'}} >
<Image source={require("./../Assets/watermelon.png")}/>
<Text style={styles.subTitle}>{subtitle}</Text>
</View>
<View style={{flex:0.75, flexDirection: 'column'}}>
<Text style={styles.title}>{title}</Text>
<Text style={styles.title}>{title}</Text>
</View>
Hope this helps.

Related

React Native Flex Two Rows having ununsual spaces

I'm trying to do some setup for flex with a flexDirection: row
But not able to get the successful outcome.
Basically it needs to be two rows without a gap. 1st row with a text label. 2nd row has 2 components (text / switch)
<View style={{
flex: 1, flexDirection:'row',
}}>
<View>
<Text style={codeModalStyles.onlyYouCanText}>
{...}
</Text>
</View>
<View style={{ width: '100%', flexDirection: 'row', justifyContent: 'space-around', padding: 14}}>
<Text style={codeModalStyles.onlyYouCanText}>
{..}
</Text>
<Switch
value={props.switchValue}
style={{transform: [{scaleX: 1.5}, {scaleY: 1.5}]}}
onValueChange={.. }
/>
</View>
</View>
But it appears like this
I tried something else like this (removing flex direction on the main container view). But i got a big space.
<View style ={{flex:1}}>
<Text style={codeModalStyles.onlyYouCanText}>
{..}
</Text>
</View>
<View style={{ width: '100%', flexDirection: 'row', justifyContent: 'space-around', padding: 14}}>
<Text style={codeModalStyles.onlyYouCanText}>
{..}
</Text>
<Switch
value={props.switchValue}
style={{transform: [{scaleX: 1.5}, {scaleY: 1.5}]}}
onValueChange={.. }
/>
</View>
</View>
Do you want like this ?
<View
style={{
flex: 1,
flexDirection: "column"
}}
>
<View>
<Text>Auto set reminders?</Text>
</View>
<View
style={{
width: "100%",
flexDirection: "row",
justifyContent: "flex-start"
//padding: 14
}}
>
<Text
style={{
width: "50%",
flexDirection: "row",
justifyContent: "space-around"
//padding: 14
}}
>
Do students need to do a new post? A reminder will be sent daily fro
3 days until they post and tag your task during posting.
</Text>
<View
style={{ flex: 1, alignItems: "center", justifyContent: "center" }}
>
<Switch
style={{ transform: [{ scaleX: 1.5 }, { scaleY: 1.5 }] }}
onValueChange={this.toggleSwitch}
value={this.state.isEnabled}
/>
</View>
</View>
</View>
sandbox

Im trying to render an object in a row but one of my images doesn't play nice with the others using flexDirection:row

Im trying to make this view with a bubble image in the margin between the left of the screen and the larger square images like the picture below
but my bubble will not stay in line with the view and
I get this
`
const Recipe = ({ name, durationLabel, numberOfServings, handlePress, handlePush }) => (
<View styles={{flexDirection: 'row'}}>
<TouchableHighlight onPress={handlePush}>
<View syle={{ justifyContent:'center'}}>
<Image
source={require("app/assets/icons/bubbleOff.png")}
style={{justifyContent:'center'}}
/>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={handlePress}>
<View style={styles.recipeTile}>
<Image
source={require("app/assets/cheesecake.png")}
style={styles.recipeImage}
/>
<View style={styles.recipeInfo}>
<View style={styles.recipeNameRow}>
<Text style={styles.recipeName}>{name}</Text>
<Image
style={{ marginRight: 10 }}
source={require("app/assets/icons/heart.png")}
/>
</View>
<View style={styles.recipeStatsRow}>
<Text style={styles.recipeStats}>
{durationLabel}
{' '}
| Serves
{' '}
{numberOfServings}
</Text>
</View>
</View>
</View>
</TouchableHighlight>
</View>
);`
my styles
`
recipeTile: {
flexDirection: "row",
flex: 1,
margin: 10,
backgroundColor: "white",
marginLeft:60,
},
recipeImage: {
width: 100,
height: 100
},`
Use justifyContent: 'center' and alignItems: 'center' to align the image center.
const Recipe = ({
name,
durationLabel,
numberOfServings,
handlePress,
handlePush,
}) => (
<TouchableHighlight onPress={handlePush}>
<View style={{ flexDirection: 'row' }}>
<View style={{ flex: 0, justifyContent: 'center', alignItems: 'center', marginLeft: 5, marginRight: 5 }}>
<Image
source={require('assets/bubbleOff.png')}
style={{ height: 20, width: 20 }}
/>
</View>
<View style={{ flexDirection: 'row', flex: 1, backgroundColor: '#FFF' }}>
<View style={{ flex: 0 }}>
<Image
source={require('assets/cheesecake.png')}
style={styles.recipeImage}
/>
</View>
<View style={{ flex: 1 }}>
<Text style={styles.recipeName}>{name}</Text>
<Text style={styles.recipeStats}>
{durationLabel} | Serves {numberOfServings}
</Text>
</View>
<Image
style={{
marginRight: 10,
height: 20,
width: 20,
position: 'absolute',
right: -20,
top: 30,
}}
source={require('assets/heart.png')}
/>
</View>
</View>
</TouchableHighlight>
);
Working demo

Why backgroundColor doesn't work with View in React Native?

I'm trying to make a top menu and colorize it, but backgroudColor property doesn't work.
How the app looks now
<View>
<View style={{
flex: 1,
height: 50,
flexDirection: 'row',
justifyContent: 'space-between',
backgroundColor: 'skyblue'
}}>
<View style={{flex: 1, backgroundColor: 'skyblue'}}><Text style={style.upMenu.text}>H</Text></View>
<View style={{flex: 1, backgroundColor: 'skyblue'}}><Text style={style.upMenu.text}>Plugins</Text></View>
<View style={{flex: 1, backgroundColor: 'skyblue'}}><Text style={style.upMenu.text}>Remote</Text></View>
<View style={{flex: 1, backgroundColor: 'skyblue'}}><Text style={style.upMenu.text}>P</Text></View>
</View>
</View>
Solved
Thanks a lot, your answers both are very useful!
Give prop flex:1 to the parent view style.
<View style={{flex:1}}>
.....Child Views
</View>
use the code below,
<View style={{ flex: 1 }}>
<View
style={{
flex: 1,
height: 50,
flexDirection: "row",
justifyContent: "space-between",
backgroundColor: "#87ceeb",
maxHeight: 20
}}
>
<View style={{ flex: 1, backgroundColor: "#87ceeb" }}>
<Text style={style.upMenu.text}>H</Text>
</View>
<View style={{ flex: 1, backgroundColor: "#87ceeb" }}>
<Text style={style.upMenu.text}>Plugins</Text>
</View>
<View style={{ flex: 1, backgroundColor: "#87ceeb" }}>
<Text style={style.upMenu.text}>Remote</Text>
</View>
<View style={{ flex: 1, backgroundColor: "#87ceeb" }}>
<Text style={style.upMenu.text}>P</Text>
</View>
</View>
</View>
to get the required header height use the maxHeight to change it

How to vertically stretch two Views that are in a column formation - React Native Flex?

I have this view, where there are two Views in a column:
<View style={styles.container}>
<View style={styles.content}>
<View style={{backgroundColor: 'orange'}}>
<Text>Test</Text>
</View>
<View style={{backgroundColor: 'yellow'}}>
<Text>Test2</Text>
</View>
</View>
</View>
This is my Styles:
container: {
flexDirection: 'row',
backgroundColor: 'white',
justifyContent:'center',
alignItems:'center',
height: 80,
margin: 8,
},
content: {
flex:1,
alignItems: 'stretch',
flexDirection: 'column',
},
Here is a ScreenShot of what it looks with the above code.
How can I get the orange and yellow View to expand vertically equally, without manually defining the height for each of them?
The Text inside should be centered, but left-aligned.
Add flex: 1 to each cell to make them expand and add justifyContent: 'center' to make the text vertically centered. Like this:
<View style={styles.container}>
<View style={styles.content}>
<View style={{ backgroundColor: 'orange', flex: 1, justifyContent: 'center' }}>
<Text>Test</Text>
</View>
<View style={{ backgroundColor: 'yellow', flex: 1, justifyContent: 'center' }}>
<Text>Test2</Text>
</View>
</View>
</View>

react native flexbox split screen

i am trying to split the screen using flexbox, but i am not getting the result i desire, here is what i have
<View style={{flex: 1}}>
<View style={{flex: 1}}>{/* half of the screen */}</View>
<View style={{flex: 1}}>{/* the other half */}
{/*<Swiper>*/}
<View style={{flex: 1}}>{/* a quarter of the other half */}</View>
<View style={{flex: 1}}>{/* a quarter of the other half */}</View>
<View style={{flex: 1}}>{/* a quarter of the other half */}</View>
<View style={{flex: 1}}>{/* a quarter of the other half */}</View>
{/*</Swiper>*/}
</View>
</View>
the problem that i am having is that the other half of the screen expands to take the size of the full screen, it is just appended to the first half without taking into consideration that is bounded by the half it exist in
screenshot
how should i approach this?
Uhm, so I kind of know what I am doing in React Native now, after over a year of learning.
<View style={{ flex: 1 }}>
<View style={{ backgroundColor: 'gray', flex: 1 }} />
<View style={{ flex: 1 }}>
<Swiper>
<View
style={{
alignItems: 'center',
backgroundColor: 'red',
justifyContent: 'center',
height: Dimensions.get('window').height / 2
}}>
<Text style={{ color: 'white' }}>Test</Text>
</View>
<View
style={{
alignItems: 'center',
backgroundColor: 'green',
justifyContent: 'center',
height: Dimensions.get('window').height / 2
}}>
<Text style={{ color: 'white' }}>Test</Text>
</View>
<View
style={{
alignItems: 'center',
backgroundColor: 'blue',
justifyContent: 'center',
height: Dimensions.get('window').height / 2
}}>
<Text style={{ color: 'white' }}>Test</Text>
</View>
</Swiper>
</View>
</View>
Try giving flexDirection: 'row' style to the outermost view.
Here's my solution: https://rnplay.org/apps/DQ2gxA
The basic idea is that you have the amount of columns (8 in this case) as a constant and then you split the available space with your needs. And use flexDirection: 'row' with the parent containers.