5

How to achieve the square layout like the image below?

Or any related package exist?

square layout

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
junlin
  • 1,835
  • 2
  • 25
  • 38
  • Use RN's own `FlatList` component together with `numOfColumns={3}` prop. You can disabled scrolling if that view is meant to be static. – Samuli Hakoniemi Nov 05 '17 at 20:22

2 Answers2

6

I was googling for the same question and found a simpler solution. Just use aspectRatio: 1 style attribute. See an example: https://reactnative.fun/2017/06/21/ratio/

Ivan Grishaev
  • 1,583
  • 10
  • 15
4

I am using the combination of ScrollView and flexbox to achieve my static grid view.

import {Dimensions} from 'react-native';

....

return (
  <ScrollView>
    <View style={styles.container}>
      {
        this.props.categories.map((category, index) => {
          return (
            <TouchableOpacity
              key={index}
              style={styles.item}
              onPress={() => {}}
            >
              <Image
                style={styles.itemIcon}
                source="..."
              />
              <Text style={styles.itemTitle}>
                {category.name}
              </Text>
            </TouchableOpacity>
          )
        })
      }
    </View>
  </ScrollView>
)

var styles = StyleSheet.create({
    container: {
        flexDirection: 'row',
        flexWrap: 'wrap'
    },
    item: {
        width: Dimensions.get('window').width * 0.5,
        height: 100,
        borderWidth: 1,
        borderColor: "lightgray",
        alignItems: 'center',
        justifyContent: 'center'        
    },
    itemIcon: {
        width: 100,
        height: 100,
        resizeMode: 'contain'
    },
    itemTitle: {
        marginTop: 16,
    },
});
Kelvin
  • 64
  • 1
  • 3