shogitai
shogitai

Reputation: 1961

How to add extra space between View elements without using CSS padding in React Native?

I am trying to beautify a bit a screen in React Native. I have some View which inside them there are Text elements. I need some more space between these Views, I tried to set alignItems: 'space-around' and other similar directives without any effect.

There is the code and below it there is a screenshot in which I drawn red arrows to indicate where I would to add some white space:

export default class UserProfile extends Component {
    render() {
        const {UserInfo} = this.props;
        return (
            <SafeAreaView>
                <View style={styles.container}>
                    <View style={styles.topView}/>
                    <View style={styles.headerView}>
                        <Text style={styles.headerText}>Welcome: {UserInfo.userName}</Text>
                    </View>
                    <View style={styles.itemView}>
                        <Text style={styles.itemTextTitle}>User ID:</Text><Text style={styles.itemText}>{UserInfo.userID}</Text>
                    </View>
                    <View style={styles.itemView}>
                        <Text style={styles.itemTextTitle}>First Name:</Text><Text style={styles.itemText}>{UserInfo.userFName}</Text>
                    </View>
                    <View style={styles.itemView}>
                        <Text style={styles.itemTextTitle}>Last Name:</Text><Text style={styles.itemText}>{UserInfo.userLName}</Text>
                    </View>
                    <View style={styles.itemView}>
                        <Text style={styles.itemTextTitle}>Status:</Text><Text style={styles.itemText}>{UserInfo.userStatus}</Text>
                    </View>
                    <View style={styles.bottomView}/>
                </View>
            </SafeAreaView>
        );
    }
}

const styles = StyleSheet.create({
    topView: {
        flex: 3
    },
    bottomView: {
        flex: 3
    },
    headerView: {flex:1 },
    headerText: {flex:1, textAlign: 'center', fontFamily: globalStyles.fonts.OpenSans},
    itemView: {flex:1 },
    itemTextTitle: {flex:1, textAlign: 'center', fontFamily: globalStyles.fonts.OpenSans, fontSize: 20, fontWeight: '500'},
    itemText: {flex:1, textAlign: 'center', fontFamily: globalStyles.fonts.OpenSans, fontSize: 18, fontWeight: '200'},
    textStyle: {
        flex:1,
        fontSize: 20,
        fontFamily: globalStyles.fonts.OpenSans,
        fontWeight: '600',
        color: globalStyles.colors.customerGreen,
        textAlign: 'center',
    },
    container: {
        display: 'flex',
        alignContent: 'center',
    },
    buttonStyle: {
        flex: 1,
        padding: 10,
        backgroundColor: '#ffffff',
        borderRadius: 7,
    }
});

enter image description here

Upvotes: 0

Views: 155

Answers (1)

B. Mohammad
B. Mohammad

Reputation: 2464

It's the SafeAreaView that stops your style from working, the container style should be on it to work.

Check this demo i made in Snack:

Demo

Upvotes: 1

Related Questions