The Complete Guide to React Native Flexbox Layout using Draftbit’s Visual Builder

8
 min read
React Native
Low Code
Mobile App Development
Programming
No Code
Aman Mittal
Sep 7, 2021

In a React Native app, the Flexbox algorithm is used to specify the layout of a component and its children. The algorithm itself is designed to provide consistency among different screen sizes. In this tutorial, I am going to introduce you to how to use various Flexbox properties to create different layout patterns using Draftbit.

What is Draftbit?

Draftbit is a visual builder for creating React Native apps. It makes creating layouts using Flexbox easier and faster than hand-coding them.

With a Draftbit account, you can build unlimited Flexbox layouts and export the screen code (all for free, no credit card required).

Building Flexbox Layouts in Draftbit

I am going to use a Blank screen template to start with that is going to wrap two components. The first child component is going to be the Text of what the example is about, and the second is going to be a View component. In this View component, you are going to see examples of different Flexbox layout properties.

An example of the template used in this tutorial with a Text and a View component. This screen is going to be modified throughout this tutorial.

The goal of this post is to give you an overview of how you can leverage the tooling provided in the Draftbit build tool. For a quick reference, all the layout properties used can be referred to in the Properties panel on the right side.

The flexDirection property

The <code-inline>flexDirection<code-inline> controls the direction of the main axis in which children components are laid out. By default, it has a value set to column along the main axis. The first step is to create three children View components inside the parent View. Each of the child components is going to have a different background color and set a <code-inline>width<code-inline> and <code-inline>height<code-inline> of <code-inline>50<code-inline>. With the default flexDirection property value, all of these child components are going to appear vertically aligned adjacently.

The <code-inline>flexDirection<code-inline> property allows the children component to be placed in the opposite direction. This is easily achievable in Draftbit as well. In the right panel, once the direction of the flex items is set, you can enable the Reverse property. The example below shows how to set the value of <code-inline>flexDirection<code-inline> to <code-inline>row-reverse<code-inline>.

Similarly, <code-inline>column-reverse<code-inline> value can be achieved.

The justifyContent property

The <code-inline>justifyContent<code-inline> controls how the children components are aligned within the main axis of their parent component. The default value of this property for each child is <code-inline>flex-start<code-inline> where a child component is aligned at the start of the parent component’s main axis.

To change the value of this property, let’s say to align all the children component at the center of the main axis, you have to change the value of Justify property under Flex Items:

You can set the value to <code-inline>flex-end<code-inline> to align the child components at the end of the parent’s main axis:

Setting the value to <code-inline>space-between<code-inline> is going to spread the children components across the parent’s main axis by evenly distributing the remaining space between them:

Setting the value to <code-inline>space-around<code-inline> is going to evenly place the children components on the parent’s main axis, distributing the remaining space around them:

The last property value is <code-inline>space-evenly<code-inline> which is used to evenly distribute the spacing between each child component.

The alignItems property

The value of <code-inline>alignItems<code-inline> property affects the children components to be aligned along with the cross or the secondary axis inside the parent component. The default value of this property is <code-inline>stretch<code-inline>. In the example below the width and height of each child component have a fixed value of <code-inline>50pt<code-inline>. This overrides the Stretch property.

The children component must not have a fixed <code-inline>width<code-inline> for this property to affect the secondary axis. The example below shows that when then width is set to auto from the right panel, the alignItems: <code-inline>stretch<code-inline> property works as expected.

The value <code-inline>flex-start<code-inline> aligns the children at the start of the parent’s secondary axis:

The value <code-inline>flex-end<code-inline> aligns the children at the end of the parent’s secondary axis:

The value <code-inline>center<code-inline> aligns the children at the center of the parent’s secondary axis:

The alignSelf property

The value of the <code-inline>align-self<code-inline> property is used to change the alignment of an individual child component by overwriting the value of <code-inline>alignItems<code-inline> set by the parent component.

For example, when the value of alignItems of the parent component is set to <code-inline>flex-start<code-inline> but the first child has an <code-inline>alignSelf<code-inline> value of <code-inline>flex-end<code-inline>. To change the assign the property of <code-inline>alignSelf<code-inline> to an individual child component, you have to select the child component and then from right panel, go to Selected Item and change the value of Align to <code-inline>flex-end<code-inline>.

Similarly, when the parent component has the value of <code-inline>flex-end<code-inline> for the <code-inline>alignItems<code-inline> property, set the <code-inline>alignSelf<code-inline> to <code-inline>flex-start<code-inline> of the first child component:

Setting the value to center for the child component is going to have the following effect:

On removing the fixed width dimension of the first child component and setting the value to <code-inline>stretch<code-inline> is going to have the following effect:

The flexWrap property

The <code-inline>flexWrap<code-inline> property wraps the children components along the main axis on multiple lines when they overflow the size of the parent component.

The default value of this property is <code-inline>nowrap<code-inline>:

The other two values are <code-inline>wrap<code-inline>:

And the <code-inline>wrap-reverse<code-inline>:

The alignContent property

The <code-inline>alignContent<code-inline> property aligns the child components across multiple lines on the secondary axis when they are wrapped using the flexWrap property. The default value of this property is <code-inline>flex-start<code-inline>.

The second value aligns the wrapped lines in the center of the parent component’s secondary axis.

The value <code-inline>flex-end<code-inline> aligns the wrapped lines at the end of the parent component’s secondary axis.

The <code-inline>space-between<code-inline> value distributes the space between the wrapped lines.

The <code-inline>space-around<code-inline> value evenly distributes the space around the wrapped lines.

The position absolute

The property <code-inline>position<code-inline> enables a child component to be positioned relatively within a parent component. The default value of this property known as <code-inline>relative<code-inline> defines each element to be positioned exactly how the flow of the layout. In the example below, the first child component has a fixed margin of <code-inline>20pt<code-inline> from <code-inline>top<code-inline> and <code-inline>left<code-inline>. The second child has a fixed margin of <code-inline>40pt<code-inline> from top and left and the third child has a fixed margin of <code-inline>60pt<code-inline> from <code-inline>top<code-inline> and <code-inline>left<code-inline>. The other two margin properties (right and bottom) are left at <code-inline>auto<code-inline>.

The <code-inline>absolute<code-inline> value allows a child component to layout independently of its siblings. The margin property for each child element remains the same from the previous example.

The zIndex property

This property can control which child component to be displayed on top of its siblings. In the example below, the second child component is displayed on top of the first and the third sibling. The value to control the position is determined by setting the <code-inline>zIndex<code-inline> to <code-inline>1<code-inline>.

To change the value of zIndex, go to the right panel, and under the property Position you will find the Z-Index field, as shown in the example below.

Conclusion

After completing this tutorial, you’ve got the overview of how to use Flexbox layout elements when building UI components using Draftbit.

Here are some links to help you out:

💜 We at Draftbit love our Community and are always open for your questions, feedback and feature requests. You will also find more resources to learn and build with Draftbit.

Reach out to us on twitter/@draftbit for more questions.

Now in open beta!