I'm trying to create a menu for my application in React-Native which should have multiple icons in the below way
The icons should be in the same row and wrapped so that if screen is bigger more icons will be on the same row.
My current code is as follows
import React from 'react';
import { StyleSheet, View } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.box}></View>
<View style={styles.box}></View>
<View style={styles.box}></View>
<View style={styles.box}></View>
<View style={styles.box}></View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'space-evenly',
flexDirection: 'row',
flexWrap: 'wrap',
paddingTop: 40
},
box: {
width: 100,
height: 100,
backgroundColor: 'aqua',
margin: 10,
}
});
The current output is as below
The children count may change in the future but i need to have spacing on the sides, using flex-start will give the below output which is wrong.i want to have spacing in both sides as well.
How do i align it to left and have the items with even space around as the image above ?
left: Used to align the text component to the left-hand side. right: align text to the right-hand side (make sure to add flex: 1). center: It align text component to the center (make sure to add flex: 1). auto: It align text automatically.
To center a component horizontally and vertically in React. js, set its display property to flex and its alignItems and justifyContent properties to center . The components's content will be horizontally and vertically centered.
for Box use Dimensions, Based on screen width divide box width
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'flex-start',
flexDirection: 'row',
flexWrap: 'wrap',
paddingTop: 40
},
box: {
width: (Dimensions.get('window').width / 3) - 20, /* minus some value for adjust the gap between boxes */
height: 100,
backgroundColor: 'aqua',
margin: 10,
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With