I have a grid-like FlatList
that is 3x3. I want the 3 columns to be distributed evenly based on the container (screen) width. In a normal view this would be accomplished by justifyContent: 'space-between'
, but this doesn't do anything when used through contentContainerStyle
.
<FlatList
contentContainerStyle={{ justifyContent: 'space-between' }}
horizontal={false}
scrollEnabled={false}
numColumns={3}
data={this.props.icons}
extraData={this.props.selectedIcon}
renderItem={this.renderItem}
keyExtractor={this.keyExtractor}
/>
As you can see the icons are all pushed to the left.
Just add horizontal padding to your container. and no matter how big your items get you will always have spacing left and right. If the item gets too big you can start to scroll.
You can give the item itself a width value of 45% . Also, flatlist has a property called columnWrapperStyle that you can give the value justifyContent: 'space-between . For me, space-around looks better than space-between.
It extracts the unique key name and its value and tells the FlatList component to track the items based on that value. For the above array of data, modify the FlatList component and use the keyExtractor prop to extract the key: <FlatList data={DATA_WITH_ID} renderItem={renderList} keyExtractor={item => item.
I solved this by passing a "columnWrapperStyle" to the FlatList:
<FlatList
data={array}
renderItem={this.renderItem}
numColumns={10}
columnWrapperStyle={style.row}
/>
const style = StyleSheet.create({
row: {
flex: 1,
justifyContent: "space-around"
}
});
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