Layouts
How do I use widgets to define layout properties?
In React Native, most of the layout can be done with the props that are passed to a specific component. For example, you could use the style
prop on the View
component in order to specify the flexbox properties. To arrange your components in a column, you would specify a prop such as: flexDirection: “column”
.
// React Native
<View
style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'space-between',
alignItems: 'center'
}}
>
In Flutter, the layout is primarily defined by widgets specifically designed to provide layout, combined with control widgets and their style properties.
For example, the Column and Row widgets take an array of children and align them vertically and horizontally respectively. A Container widget takes a combination of layout and styling properties, and a Center
widget centers its child widgets.
// Flutter
Center(
child: Column(
children: <Widget>[
Container(
color: Colors.red,
width: 100.0,
height: 100.0,
),
Container(
color: Colors.blue,
width: 100.0,
height: 100.0,
),
Container(
color: Colors.green,
width: 100.0,
height: 100.0,
),
],
),
)
Flutter provides a variety of layout widgets in its core widget library. For example, Padding
, Align
, and Stack
.
For a complete list, see Layout Widgets.
Android |
IOS |
How do I layer widgets?
In React Native, components can be layered using absolute
positioning.
Flutter uses the Stack
widget to arrange children widgets in layers. The widgets can entirely or partially overlap the base widget.
The Stack
widget positions its children relative to the edges of its box. This class is useful if you simply want to overlap several children widgets.
// Flutter
Stack(
alignment: const Alignment(0.6, 0.6),
children: <Widget>[
CircleAvatar(
backgroundImage: NetworkImage(
'https://avatars3.githubusercontent.com/u/14101776?v=4'),
),
Container(
decoration: BoxDecoration(
color: Colors.black45,
),
child: Text('Flutter'),
),
],
)
The previous example uses Stack
to overlay a Container (that displays its Text
on a translucent black background) on top of a CircleAvatar
. The Stack offsets the text using the alignment property and Alignment coordinates.
Android |
IOS |
For more information, see the Stack class documentation.