How to create a vertical progress path in React Native? - react-native

how do I create a vertical progress path in React Native? Please refer to the first image below for the current design that I have and to the second image below for the output that I want (I want it with lines).
Here is a clearer example from Google

I managed to solve this in two ways. My first idea was to have for every step (row) two elements, one for the indicator and one for the actual information for the step. This solution has some spacing issues but still isn't that bad. To fix the spacing issues I came up with solution number two.
Solution 1 - absolute position the line
return (
<View style={styles.stepContainer}>
{/* the first element for the indicator and the line */}
<View style={styles.stepIndicator}>
{i < steps.length - 1 ? <View style={styles.stepLine}></View> : null}
<Text style={styles.stepIndicatorText}>{i + 1}</Text>
</View>
{/* the second element for the actual step and its information */}
<View style={styles.step}>
<Text>{step.name}</Text>
</View>
</View>
);
The idea here is to push the stepLine down by x and don't render the line on the last step since that will cause some overflow.
This approach works quite well but the issue comes when you're trying to add spacing to the steps, e.g. marginBottom. The line won't connect anymore since it is limit by the height of their row. You could hard code the amount of spacing to the height of the line but that gets unmanageable very quickly. For this problem I found solution 2.
Solution 2 - two columns
<View
style={{
flexDirection: "row",
}}
>
{/* the column for the line */}
<View style={styles.stepLineContainer}>
<View style={styles.stepLine}></View>
</View>
{/* the column for the steps */}
<View
style={{
flex: 1,
gap: 8,
}}
>
{steps.map((step, i) => (
<View style={styles.stepContainer}>
{/* the indicator */}
<View style={styles.stepIndicator}>
<Text style={styles.stepIndicatorText}>{i + 1}</Text>
</View>
<View style={styles.step}>
<Text>{step.name}</Text>
</View>
</View>
))}
</View>
</View>
This solution involves two columns. One for the progress line and one for the steps. The idea here is to have the line in the left column which, with flexbox, will have the same height as the steps column. To have the indicators in the correct place we can place them on the actual step and give them a position: "absolute". Now we can add spacing to the steps by using marginBottom or even better the gap property.
Here is a live preview of both solutions.

Related

React Native | Default Set Width Style

I noticed that while adding 'View' with React native, it behaves as if "width: '100%'" was assigned even though I did not enter any style. Same situation exists for any component. It covers the whole area horizontally. What is the cause and solution?
return (
<View style={{flex:1}}>
{/* paints the line he is in full red but should only paint as much as the line of the text */}
<View style={{backgroundColor:'red'}}>
<Text>Example</Text>
</View >
</View>
);
If you want the background color to only encompass the text, you can use this:
<Text style={{backgroundColor: 'red', alignSelf: 'flex-start'}}>
Example
</Text>

How to put another line of text immediately after another line of text?

I'm trying to place a line of text immediately after another line of text, however react-native is creating a lot of unneeded space between the two. I think this may be because of the top line's big font size.
I am trying to get my application to look like this
However, my page looks like this. Notice there is a large gap between the number and "Total Vehicle".
Here's my source code I tried using paddingBottom for the second line of text, but that did not end up working.
<TouchableHighlight onPress = { () => alert('hello')}>
<ImageBackground style={{width: '100%', height: '100%'}} source={require('../cars.jpg')} >
<View style = {{alignItems:'center', justifyContent:'center',flex:1, paddingBottom:15}}>
<Text style = {{color:'#E31E24', fontSize:120}}> {this.props.total} </Text>
<Text style = {{color:'white'}}> TOTAL VEHICLE </Text>
</View>
</ImageBackground>
</TouchableHighlight>
you can use lineHeight or marginTop(negetive) in the style of second text to move up that.
i wish it work for you.

How to put two components at right and left of the screen

I need to put two components at two ends of the screen. Following is my code.
<View style = {{flexDirection:"row"}}>
<Text>cancel</Text>
<Text style={{justifyContent:"flex-end",alignItems:"flex-end"}}>cancel</Text>
The two buttons are near to each other and I want to push the second button to the extreme right end of the screen without margin, how to do it by properties? It is not working currently. Thank you.
You need to do something like this
<View style={{flex:1,flexDirection:"row",justifyContent:"space-between"}}>
<View style={{flex:0.5}}>
<Text>Cancel 1</Text>
</View>
<View style={{flex:0.5,alignItems:"flex-end"}}>
<Text>Cancel 2</Text>
</View>
</View>

How to fixed a <Text> in a scrollView?

My all screen has a scrollView, and there's a in the middle of it. I want when someone scroll up, the text be fixed at the top and does not disappear. How can I do that? I didn't find it anywhere, thanks.
<View style={styles.container}>
<Text style={{ position : 'absolute', textAlign: 'center'}}>
Special Text
</Text>
<ScrollView>
</ScrollView>
</View>
you can do something like this
Use css property position: sticky. that will fixed your text at the top it's called sticky position. So, Please read it https://www.w3schools.com/css/tryit.asp?filename=trycss_position_sticky
Be patient.
Cheer you!
i was trying to implement something similar and i got it done but unfortunately it is kinda complex (relatively) at least compared to what you would do on the web. it requires using the onLayout prop.
state = {zIndex: 0};
<View>
<View style={[your_style, {position: "relative", zIndex: this.state.zIndex}]} onLayout={({nativeEvent}) => {this.outTextHeight = nativeEvent.layout.height}}>
<Text>Special Text</Text>
</View>
<View style={{[your_style, {zIndex: 1}]}>
<ScrollView onScroll={({nativeEvent}) => {if(nativeEvent.contentOffset.y >= (this.insideTextHeight + this.insideTextOffsety){this.setState({zIndex: 2})})}}>
// your content
<View onLayout={({nativeEvent}) => {this.insideTextHeight = nativeEvent.layout.height; this.insideTextOffsety = nativeEvent.layout.y}}>// this maybe the same height so may be unnecessary
<Text>Special Text</Text>
</View>
</ScrollView>
</View>
</View>

Flexbox align images with different size to fill the area

I am using React Native's flexbox (not css flexbox). I am creating an image gallery and the first image has to be double the size, and the rest of the images smaller. Works good, but I have a problem that the third image is displayed in new row, instead where the blank space is.
Is it possible to achieve such behaviour with flex-box, so that the third image would be below the first small image?
I tried all combinations with aligning items, self aligning, flex directions, but no success. If needed I can provide a small example of the code which I already have.
I don't have a fully responsive answer, but this may be helpful here:
<View style={{ flexDirection: 'column' }}>
<View style={{ flexDirection: 'row' }}>
{this.renderPhoto(0)}
<View>
{this.renderPhoto(1)}
{this.renderPhoto(2)}
</View>
</View>
<View style={{ flexDirection: 'row' }}>
{render rest...}
</View>
</View>
Try this component. Maybe it will help you
https://xudafeng.github.io/autoresponsive-react/