I'm trying to use a FlatList to show a bunch of user avatars to someone in a grid format, but it ends up looking very strange and I can't seem to figure out how to fix it.
Here's what it looks like
Here's what my FlatList code looks like:
<FlatList
style={{flex: 1}}
data={this.props.usersList}
horizontal={false}
numColumns={3}
columnWrapperStyle={{ marginTop: 10 }}
renderItem={({ item }) => this.renderItem(item)}
keyExtractor={this._keyExtractor}/>
and here's what the component looks like for renderItem:
class UserButton extends React.Component {
render() {
const { item, onPress } = this.props;
return (
<TouchableOpacity style={styles.button} onPress={onPress}>
<Image
source={(item.avatar) ? { uri: item.avatar } : require('../assets/images/userplaceholder.png')}
resizeMode='cover'
style={styles.imageStyle}
/>
</TouchableOpacity>
)
}
const styles = {
button: {
height: 100,
width: 100,
borderColor: '#aaa',
backgroundColor: '#aaa',
borderWidth: 2,
borderRadius: 50,
justifyContent: 'center',
alignItems: 'center',
marginHorizontal: 5,
},
imageStyle: {
height: 96,
width: 96,
alignSelf: 'center',
borderRadius: 48,
marginTop: (Platform.OS == 'android') ? 0 : 0.4
}
}
export default UserButton;
Anyone have any ideas?
You can take width from Dimensions and set that width for items of your flatlist.
const {width} = Dimensions.get('window');
const itemWidth = (width) / 4;
I was in kind of similar situation, but now I have proper grid using flatList, you can take a look at my code attached below.
<FlatList
contentContainerStyle={{margin:4}}
horizontal={false}
numColumns={4}
data={this.state.categoryDataSource}
renderItem={(categoryItem) =>
<CategoryListItem category={categoryItem.item} mode="small"/>
}
keyExtractor={category => category.id}
/>
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