React native flexbox how to align items vertically? - css

I'm a bit confused as to why this flex won't work.
<View
style={{
display: "flex",
flexWrap: "wrap"
}}
>
<View style={{ width: "40%", alignSelf: "flex-start" }}>
<Button BUY</Button>
<View style={{ alignSelf: "center", marginTop: "2%" }}>// A logo</View>
</View>
<View style={{ width: "40%", alignSelf: "flex-end" }}>
<AreaChart
style={{ height: 250 }}
dataPoints={data}
contentInset={{ top: 30, bottom: 30 }}
curve={shape.curveNatural}
svg={{
fill: "rgba(134, 65, 244, 0.2)",
stroke: "rgb(134, 65, 244)"
}}
/>
</View>
</View>;
The intended layout I want to have is:
BUTTON | <CHART
LOGO | CHART>
The button and logos centered together, and the chart taking up both "boxes" on the right.
However, the above markup produces the following:
BUTTON |
LOGO |
| CHART
| CHART
They're doing the right thing but the Chart starts where the logo ends.
Where did I go wrong with flexbox? How do I produce the right layout?

As the default flexDirection in React Native is column, flex items will stack vertically, hence the chart renders on a row of its own.
By using alignSelf: "flex-end" on the 2nd child View, which for columns control the cross axis (horizontal), it will right align.
To align them side-by-side, add flexDirection: "row" to the parent View.
You can also remove both the alignSelf properties on the two child View's, unless you want the 2nd View (alignSelf: "flex-end") align to bottom.
Updated code sample
<View style={{
display: "flex",
flexDirection: "row",
flexWrap: "wrap"
}}>
<View style={{width: "40%"}}>
<Button
BUY
</Button>
<View style={{alignSelf: "center", marginTop: "2%"}}>
// A logo
</View>
</View>
<View style={{ width: "40%"}}>
<AreaChart
style={{height: 250}}
dataPoints={data}
contentInset={{top: 30, bottom: 30}}
curve={shape.curveNatural}
svg={{
fill: 'rgba(134, 65, 244, 0.2)',
stroke: 'rgb(134, 65, 244)',
}}
/>
</View>
</View>

Related

How to fix the footer on bottom of the page in react-native?

I want to fix the footer on the bottom of the page,
Thats why my whole container was in flex:1 so it takes all the space,
and in my footer, i added position:"absolute",marginBottom:0,, the footer affected so badly which i didnt expect. It goes to up like in the header of the page, then i deleted it,
How can i fix it on bottom of page?
Here is the related code.
<SafeAreaView style={{ flex: 1, backgroundColor:"#ffffff", }}>
{/* Footer */}
<View style={{ flexDirection:"row", alignItems:"center", justifyContent:"space-between",marginHorizontal:35,}}>
<View> // here there are some code
<View>
<Home height={30} width={22} fill={"#1E2439"} />
<Ratio height={30} width={22} fill={"#1E2439"} />
<Time height={30} width={22} fill={"#1E2439"} />
<User height={30} width={22} fill={"#1E2439"} />
</View>
</View>
</SafeAreaView>
Here how the page seen:
Position is from the css language.
{
position:"absolute",
bottom:0
}
here is my code for fixed footer
<View style={{flex:1}}>
<ScrollView style={{flex:1}}>
/// some Code
</ScrollView>
// here is bottom bar
<View style={{height: 60, justifyContent: 'center', borderTopLeftRadius: 20, borderTopRightRadius: 20, flexDirection: 'row', alignContent: 'center', alignItems: 'center'}}>
<View style={{flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center'}}>
//Here is you bottom icon or image
<View>
</View>
</View>
The position is from the CSS language.
{
position:"absolute",
bottom:0,
height: 60
}

Partially changing background color of a <View> component in React Native based on %

So I have these Views in react native I am using as cards, and I want to partially change the background of the view based on a %
So for example I would find what % 230 is of 1000 and I would fill the background of the card that % to a different color. Anyone have any suggestions on doing the background color with a % like that? I know I can fill it with border and border width but the issue is that pushes the content out of the way to fill it. I need the content to stay in place but the background color change partially based on the %. I also tried making another View overtop of this one and adding the border and border width and filling the top one, but that covers the content of the bottom one, which doesnt really help either.
Not sure a code snippet is needed for this question but screw it:
<View key={value.title + index} style={{ backgroundColor: 'white', margin: 10, borderRadius: 5, width: '90%', height: 70, alignSelf: 'center', justifyContent: 'center' }}>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<View style={{ margin: 15 }}>
<IconX
size={30}
origin={ICON_TYPE.FONT_AWESOME5}
name={'piggy-bank'}
color={'black'}
/>
</View>
<View style={{ flexDirection: 'column', width: 125 }}>
<Text style={{ fontWeight: 'bold' }}>{value.title}</Text>
{value.date && (
<Text>{value.date.toDateString()}</Text>
)}
</View>
<View style={{ flexDirection: 'column', marginLeft: '-5%' }}>
<View style={{ width: 150 }}>
<Text style={{ fontWeight: 'bold', textAlign: 'right', color: '#F36A53' }}> ${value.currentAmount} / ${value.goalAmount}</Text>
</View>
</View>
</View>
</View>
I ended up just adding another card overtop of it and just using a transparent border color so it didnt cover the content, but I am not totally happy with this solution, so am open to any other answers. Thanks.

How to center text on the screen when it's width is not 100% [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 2 years ago.
I want to center the second piece of text. The first piece has an icon that takes up part of the width. The second text doesn't take up the full width of the screen, and so when I apply the textAlign to center, the text center should consider its width.
There is another text below the first that takes the full width, so it's really aligned in the center of the screen instead of being center relative the the first piece of text above it.
code:
<View style={{ marginBottom: 25 }}>
<View style={{ flex: 1, flexDirection: "row", alignItems: "center", marginTop: 15 }}>
<MaterialIcons onPress={() => console.log("funciona por favor!!")} name="close" size={20} style={{ color: "#FFF" }} />
<Text style={{ flex: 1, color: "#FFF", fontSize: 24, textAlign: "center" }}>Configurações</Text>
</View>
<Text style={{ color: COLORS.white1, fontSize: 24, marginTop: 0, alignSelf: "center" }}>Configurações</Text>
</View>
What I tried:
The icon has 30px of width, so I tried a negative value margin for the text and to center it, but it covers the icon, and this icon needs to be pressed, so the press doesn't work.
Thanks for the help, I hope I have been clear in exposing the problem.
You can add a margin to the right of the content to offset the amount the text it pushed from the right.
<View style={{ marginBottom: 25, backgroundColor: 'grey' }}>
<View style={{ flex: 1, flexDirection: "row", marginTop: 15 }}>
<MaterialIcons name="close" size={20} style={{ color: "#FFF" }} />
<Text style={{ flex: 1, color: "#FFF", fontSize: 24, textAlign: "center", marginRight: 20 }}>Configurações</Text>
</View>
<Text style={{ color: '#fff', fontSize: 24, marginTop: 0, alignSelf: "center" }}>Configurações</Text>
</View>
Make your button a grid:
div.button {
background:blue;
padding:4px;
color:white;
font-family:"Verdana";
width:50vw;
display:grid;
grid-template-columns: 5% auto 5%;
}
div.button div.caption {
text-align:center;
}
<div class="button">
<div class="left">ICON</div>
<div class="caption">Your caption here</div>
<div class="right"></div>
</div>
Using this approach, you define three columns, put your icon into the leftmost one, the caption into the middle one, and leave one column free which is as wide as the left one, making the center column actually being centered.
You wrap your close icon in absolute view and set the left position accordingly. In this way, your text will be aligned with the below text
<View style={{position:'absolute',left:0}}>
<MaterialIcons onPress={() => console.log("funciona por favor!!")} name="close"
size={20} style={{ color: "#FFF" }} />
</View>

react native back ImageBackground centering

I have an image where i try to center some text on it.
For that i use ImageBackground to have child elements of the image.
However the image is being scaled down on small devices, and full size if possible
When i center my text, it gets centered after the whole image ratio ( on a big screen where you can see the whole image, its centered - on small screens its offset because some of the image is cropped to fit.
<ImageBackground style={{ width: wp('50%'),
height: hp('50%'),
resizeMode: 'cover',
justifyContent: 'center',
alignItems: 'center' }}
source={require('../assets/images/12.jpg')}>
// if the full image is showing - it's centered, otherwise not!
<Text style={{ color: "red" }}>Centered text</Text>
</ImageBackground>
you need to add an extra view to wrap the ImageBackground and add style property
justifyContent and alignItems to center and I think this will erase your problem.
<View style={{ flex:1 , justifyContent: 'center', alignItems: 'center' }}>
<ImageBackground
style={{ width: wp('50%'), height: hp('50%'), resizeMode: 'cover', justifyContent: 'center', alignItems: 'center' }}
source={require('../assets/images/12.jpg')}>
// if the full image is showing - it's centered, otherwise not!
<Text style={{ color: "red" }}>Centered text</Text>
</ImageBackground>
</View>

Vertically Stacked Views with Static and Dynamic Heights - React Native

I am trying to acheive what the image below represents with Flexbox in React Native with Views, if you have a basic example of this that would be amazing:
Something like this:
<View style={{ flex: 1, flexDirection: 'column'}}>
<View style={{ height: 70, backgroundColor: 'blue' }}>
</View>
<View style={{ height: 70, backgroundColor: 'green' }}>
</View>
<View style={{ flex: 1, backgroundColor: 'yellow' }}>
</View>
</View>
Not sure if the blue lines are lines / padding / etc, but you should be able to add those into this shell.

Resources