Search code examples
cssreact-nativeflexboxreact-native-flexbox

alignItems: 'flex-end' doesn't work properly (it doesb't cover the whole page) - react native


So in my homepage I have 4 buttons which I've laid out with flex. I've set the parent's flex: 1, which means it's covering the whole page (I've made sure of that with backgroundColor). My problem is that when I set alignItems: 'flex-end', the buttons only move down a little, as if the flex is covering only half the page.

Here's my code Mark-up:

<Card style={styles.cardStyle}>
      <CardSection style={styles.containerStyle}>
        <Button onPress={() => navigate("NewScreen")}>
          New
        </Button>
      </CardSection>

      <CardSection style={styles.containerStyle}>
        <Button onPress={() => navigate("SavedScreen")}>
          Saved
        </Button>
      </CardSection>

      <CardSection style={styles.containerStyle}>
        <Button onPress={() => navigate("ParametersScreen")}>
          Settings
        </Button>
      </CardSection>

      <CardSection style={styles.containerStyle}>
        <Button onPress={() => navigate("FMRScreen")}>
          FMR
        </Button>
      </CardSection>

    </Card>

Card Style:

cardStyle: {
    flex: 1,
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: '#0000ff',
}

CardSection style:

containerStyle: {
    borderBottomWidth: 1,
    padding: 5,
    backgroundColor: '#fff',
    borderColor: '#ddd',
    height: 150,
    width: 150,
    borderRadius: 20,
    marginTop: 10,
},

And the style for the items:

textStyle: { 
    color: '#007aff',
    fontSize: 20,
    fontWeight: '600',
},
buttonStyle: {
    backgroundColor: 'rgba(255, 255, 255, 0)',
    borderWidth: 0,
    borderColor: '#007aff',
    flex: 1,
    flexDirection: 'column',
    justifyContent: 'center',
    alignItems: 'center',
},

And this is what I get:

enter image description here

Note that this problem goes away if I remove flexWrap: 'wrap', but I can't do this!

Any ideas?


Solution

  • You need to do something like this to make that work properly, where it is the <Card> element being the outer most flex parent of the flex items.

    Note the added alignContent: 'flex-end', which is needed when flex items wrap

    <Card style={styles.containerStyle}>
    
      <CardSection style={styles.sectionStyle}>
        <Button onPress={() => navigate("NewScreen")}>
          New
        </Button>
      </CardSection>
    
      <CardSection style={styles.sectionStyle}>
        <Button onPress={() => navigate("SavedScreen")}>
          Saved
        </Button>
      </CardSection>
    
      <CardSection style={styles.sectionStyle}>
        <Button onPress={() => navigate("ParametersScreen")}>
          Settings
        </Button>
      </CardSection>
    
      <CardSection style={styles.sectionStyle}>
        <Button onPress={() => navigate("FMRScreen")}>
          FMR
        </Button>
      </CardSection>
    
    </Card>
    
    containerStyle: {
        flex: 1,
        flexDirection: 'row',
        flexWrap: 'wrap',
        justifyContent: 'space-around',
        alignItems: 'flex-end',
        alignContent: 'flex-end',
        backgroundColor: '#0000ff',
    }
    
    sectionStyle: {
        borderBottomWidth: 1,
        padding: 5,
        backgroundColor: '#fff',
        borderColor: '#ddd',
        height: 150,
        width: 150,
        borderRadius: 20,
        marginTop: 10,    
    }