javascriptreactjsreact-nativevector-icons

Icon causes gap in View - React Native


I have a ListView that looks as such:

screenshot

As you can see, the dropdown arrow Icon creates a gap on the left side of each item just above the first bit of text. I want to avoid having this gap, current code:

<Item key={i+tk}>
                <TouchableOpacity onPress={() => this.setState({prevIndex: this.state.activeIndex, activeIndex: i+tk,
                    messageJustClosed:  (this.state.prevIndex === i+tk  && !this.state.messageJustClosed)})} style={{flex: 1}}>
                <View>
                    <Icon  name= {this.getIconImage(i+tk)} size={30} color="black" style={{alignSelf: 'flex-end'}}/>

                    <Text style = {styles.text}>{messageType[i].title}</Text>
                  <Text style = {styles.dateText}>{new Date(messageType[i].created).toString().substring(0,21)}</Text>
                  <Expand value={ ((this.state.activeIndex === i+tk) && !(this.state.prevIndex === i+tk) ) || ((this.state.activeIndex === i+tk && this.state.messageJustClosed)) }>
                    <Text style = {{color: 'black'}}>{messageType[i].message}</Text>
                  </Expand>
                </View>

                </TouchableOpacity>
            </Item>

I tried placing it inline with the tag as such:

<Text style = {styles.text}>{messageType[i].title} <Icon  name= {this.getIconImage(i+tk)} size={30} color="black" style={{alignSelf: 'flex-end'}}/> </Text>

This gets rid of the gap, but doesn't place it in the same place (to the right of the screen). I tried giving the Text tag a flex of 1 but that messed things up completely, is there a way I can get the Icon inline with the text and all the way to the right?


Solution

  • Your <View> that contains the icons and the cell content is rendering with the default flexDirection: column, so if you change to flexDirection: row you can accomplish what you're after.

    Here's an example of how to compose your JSX:

    <View style={{ flexDirection: "row" }}>
            <View style={{ flex: 1 }}>
              <Text style={styles.text}>{messageType[i].title}</Text>
              <Text style={styles.dateText}>{new Date(messageType[i].created).toString().substring(0, 21)}</Text>
              <Expand
                value={
                  (this.state.activeIndex === i + tk && !(this.state.prevIndex === i + tk)) ||
                  (this.state.activeIndex === i + tk && this.state.messageJustClosed)
                }
              >
                <Text style={{ color: "black" }}>{messageType[i].message}</Text>
              </Expand>
            </View>
            <Icon name={this.getIconImage(i + tk)} size={30} color="black" />
          </View>