Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Center Components Using Flex in React Native?

I'm with difficulties to centralize my components in screen using CSS.

See my App.js:

import { Container } from 'native-base';
import React from 'react';
import { StyleSheet } from 'react-native';

import Screen from './ScreenContainer';

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#FF6666',
    flex: 1
  }
});

const App = () => (
  <Provider store={store}>
    <Container style={styles.container}>
      <Screen />
    </Container>
  </Provider>
);

export default App;

Now, see my ScreenContainer.js

import { Container, Content, Form, Input, Label, Item } from 'native-base';
import React from 'react';

import AppLogo from '../components/AppLogo';

const Screen = () => (
  <Container>
    <Content>
      <AppLogo />
      <Form>
        <Item floatingLabel last>
          <Label>Username</Label>
          <Input />
        </Item>
      </Form>
    </Content>
  </Container>
);

export default Screen;

This code results in this screen:

enter image description here

But, I want that the screen stay with this format:

enter image description here

When I change the code for:

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
    backgroundColor: '#FF6666',
    flex: 1
  }
});

My App returns this screen:

enter image description here

like image 465
FabianoLothor Avatar asked Apr 22 '18 23:04

FabianoLothor


People also ask

How do you center elements in React Native?

You can either use class="row" of can give inline styles like style={{ display: 'flex, justContent: 'center', alignItems: 'center' this will perfectly align everything in center of the parent div.

How do you center elements inside Flex?

To center the inner div element we will make the parent a flex container. By adding the display: flex; property we make the section element a flex container allowing us to adjust the layout of the div which is now a flex item. To center out item horizontally we use the justify-content: center; .

How do you use flex in React Native?

In React Native, Flexbox is the same as it is for basic CSS. The only difference being in default values. flexDirection: The default value for CSS is row whereas the default value in React Native is column. alignContent: The default value for CSS is stretch the default value in React Native is flex-start.

How can you center your flex items along the main axis?

To center our box we use the align-items property to align our item on the cross axis, which in this case is the block axis running vertically. We use justify-content to align the item on the main axis, which in this case is the inline axis running horizontally.


1 Answers

It's Done:

import { Container, Content, Form, Input, Label, Item } from 'native-base';
import React from 'react';
import { StyleSheet } from 'react-native';

import AppLogo from '../components/AppLogo';

const styles = StyleSheet.create({
  container: {},
  content: {
    alignItems: 'center',
    flex: 1,
    justifyContent: 'center'
  },
  form: {
    width: '100%'
  },
  item: {}
});

const Screen = () => (
  <Container style={styles.container}>
    <Content contentContainerStyle={styles.content}>
      <AppLogo />
      <Form style={styles.form}>
        <Item floatingLabel last>
          <Label>Username</Label>
          <Input />
        </Item>
      </Form>
    </Content>
  </Container>
);

export default Screen;
like image 144
FabianoLothor Avatar answered Sep 29 '22 06:09

FabianoLothor