Search code examples
react-nativereact-native-elements

failed to align checkbox in react-native elements


I am new to react-native,I am trying to create a simple page using all input components from react-native elements 1.00 beta4.My problem is I failed to align checkbox near my text component even if the parent view flexdirection is in 'row'.

register.js

import React,{ Component } from 'react';
import { Field,reduxForm } from 'redux-form';
import { View,Button } from 'react-native';
import {Icon,CheckBox,Text,Input} from 'react-native-elements';
 const renderField=({label,keyboardType,name,icon,iconType}) => {
    return(
            <View style={{flexDirection:'row'}}>
                <Input placeholder={label} inputContainerStyle={{borderWidth:2,borderColor:'lightgrey',borderRadius:20}} inputStyle={{color:'grey'}} leftIcon={<Icon size={25} type={iconType} name={icon} color="grey" />} errorStyle={{fontSize:15}} errorMessage="error" />
            </View>
    )
}
 const checkBoxField=({label,keyboardType,name}) => {
    return(
            <View style={{flexDirection:'row'}}>
                <Text>{label}</Text>
                <CheckBox  title='Male' checkedIcon='dot-circle-o' uncheckedIcon='circle-o' checked={true} containerStyle={{backgroundColor:'transparent'}} />      
            </View>
    )
}

const RegisterForm=props => {
    const {handleSubmit}=props;
    return(
            <View style={{flex:1,flexDirection:'column',margin:20,justifyContent:'flex-start',alignItems:'center'}}>

                <Field label="Username" component={renderField} name="username" icon="user" iconType="font-awesome" />
                <Field label="Email" component={renderField} name="username" icon="email" iconType="zocial" />
                <Field label="Gender" component={checkBoxField} name="gender"  />
                <Button title='SUBMIT' onPress={handleSubmit} />

            </View>
    )
}
const Register=reduxForm({
    form:'register',
})(RegisterForm);
export default Register;

output

enter image description here

what is the problem here?How can I align my checkbox (using flexbox algorithm)


Solution

  • you can use flex property like justifyContent and alginItem or you can also use Text property like alignText, and alignSelf.

     const checkBoxField=({label,keyboardType,name}) => {
        return(
                <View style={{flexDirection:'row',alignItems:'center',justifyContent:'center'}}>
                    <Text style={{alignSelf:'center',textAlign:'center'}}>{label}</Text>
                    <CheckBox  title='Male' checkedIcon='dot-circle-o' uncheckedIcon='circle-o' checked={true} containerStyle={{backgroundColor:'transparent'}} />      
                </View>
        )
    }