- In Flutter, a layout is essentially the way in which you arrange widgets on the screen. Widgets are the building blocks of a Flutter user interface. They can be anything from buttons and text fields to images and videos.
Layouts allow you to arrange these widgets in a way that makes sense for your user interface. There are several built-in layout widgets in Flutter that you can use to create different arrangements of widgets. For example, you might use a Column layout to stack widgets vertically, or a Row layout to place them side-by-side horizontally.
Each layout widget has its own set of properties that you can use to customize its behavior. These properties can include things like padding (the space around a widget), alignment (where a widget is placed within its container), and spacing (the distance between widgets).

By using different combinations of layout widgets and properties, you can create complex and responsive user interfaces in Flutter that work on a variety of different devices and screen sizes.
There are several built-in layout widgets in Flutter, such as:
Column Layout.
In Flutter, the Column layout widget is used to arrange its children widgets vertically, in a single column. It is one of the most commonly used layout widgets in Flutter, and is often used to create scrolling lists or to stack widgets on top of each other.
The Column widget takes in a list of children widgets as its input, and arranges them vertically, one after another, from top to bottom. You can specify additional properties to customize the layout, such as the vertical alignment of the children, the spacing between the children, and the amount of padding around the children.
Here’s an example of Column Widget.
Column(
children: <Widget>[
Text('Hello'),
Text('World'),
],
)
Row Layout
On the contrary to column, the Row layout widget is used to arrange its children widgets horizontally, in a single row. It is one of the most commonly used layout widgets in Flutter, and is often used to create horizontal navigation bars, button bars, or to display icons alongside text. The Row widget takes in a list of children widgets as its input, and arranges them horizontally, one after another, from left to right. You can specify additional properties to customize the layout, such as the horizontal alignment of the children, the spacing between the children, and the amount of padding around the children.
Here’s an example of Row Widget.
Row(
children: <Widget>[
Text('Left'),
Text('Center'),
Text('Right'),
],
)
Stack Layout
The Stack layout widget is used to position its children widgets relative to its edges or the edges of another child. This allows you to overlay multiple widgets on top of each other, and control the position and size of each widget within the stack.
In a Flutter app, you might use a Stack layout widget to create an overlay, such as a popup dialog box, a tooltip, or a button that appears when you tap an image. You can also use the Stack layout to layer widgets on top of each other, such as placing text on top of an image, or placing an icon on top of a button.
Here’s an example of Stack Widget.
Stack(
children: <Widget>[
Text(
'Hello',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
Positioned(
top: 30,
left: 0,
child: Text(
'World',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.normal,
),
),
),
],
)
Expanded Layout
The Expanded widget is used to expand its child widget to fill the available space within its parent widget. This is useful when you want to create a layout where one widget takes up most of the available space, while the other widgets take up less space or are smaller in size.
The Expanded widget works by instructing its parent widget to give it as much space as possible, based on the available space and the other widgets in the parent widget. The parent widget then adjusts the size of the Expanded widget to fill the available space, while ensuring that the other widgets in the parent widget are also given enough space.
Here’s an example of Expanded Widget in Row Widget.
Note: Expanded widget in Row widget expands a child in its mainAxisAlingment, and same for the Column widget. MainAxisAlignment of Row widget is horizontal and ManinAxisAlignment of Column widget is vertical.
Row(
children: <Widget>[
Text('Left'),
Expanded(
child: Text('Center'),
),
Text('Right'),
],
)
GridView
The GridView widget in Flutter is a layout widget that is used to arrange its children widgets in a grid format. This means that you can display multiple widgets in a grid-like pattern, with a specified number of columns and rows.
To create a GridView widget in Flutter, you can use the GridView.count constructor or the GridView.builder constructor. The GridView.count constructor allows you to specify the number of columns in the grid, while the GridView.builder constructor is useful when you want to create a scrolling grid with a dynamic number of items.
In addition to specifying the number of columns in the grid, you can also customize the spacing between the columns and rows using the mainAxisSpacing and crossAxisSpacing properties, respectively. This allows you to create a grid layout that looks exactly the way you want it to.
You can add any type of widget as a child of the GridView widget, such as images, text, buttons, or custom widgets. The GridView widget will automatically arrange the children in a grid format, based on the number of columns and other properties you’ve specified.
The GridView widget is a powerful tool for creating user interfaces in Flutter that display a large amount of content in a structured and organized way. By using the GridView widget, you can create responsive and flexible grid layouts that adapt to different screen sizes and orientations, while also giving users a visually appealing and intuitive way to interact with your app.
Here’s an example of GridView widget.
GridView.count(
crossAxisCount: 2,
children: List.generate(4, (index) {
return Center(
child: Text('Item $index'),
);
}),
)
ListView
The ListView widget is a layout widget in Flutter that displays a scrolling list of child widgets. You can add any type of widget as a child of the ListView widget, such as images, text, buttons, or custom widgets, and the ListView widget will automatically scroll the list as needed.
ListView(
children: <Widget>[
ListTile(
title: Text('Item 1'),
),
ListTile(
title: Text('Item 2'),
),
ListTile(
title: Text('Item 3'),
),
ListTile(
title: Text('Item 4'),
),
ListTile(
title: Text('Item 5'),
),
],
)
Wrap Layout
The Wrap widget is a layout widget in Flutter that arranges its children widgets in a wrapped layout, allowing them to flow onto multiple lines if necessary. This means that you can display multiple widgets in a row or column, and if there isn’t enough space, the Wrap widget will automatically move the remaining widgets to the next line or column.
To create a Wrap widget in Flutter, you can use the Wrap constructor and add child widgets as needed.
Wrap(
spacing: 8.0, // Set the spacing between the children
runSpacing: 4.0, // Set the spacing between the rows
children: [
Chip(
label: Text('Flutter'),
backgroundColor: Colors.blue,
),
Chip(
label: Text('Dart'),
backgroundColor: Colors.blue,
),
Chip(
label: Text('Mobile App Development'),
backgroundColor: Colors.blue,
),
Chip(
label: Text('UI Design'),
backgroundColor: Colors.blue,
),
Chip(
label: Text('Cross-platform Development'),
backgroundColor: Colors.blue,
),
],
)
In this example, we’re using the Wrap
widget to create a horizontal list of Chip
widgets. The spacing
property sets the amount of space between the Chip
widgets, and the runSpacing
property sets the amount of space between each row of Chip
widgets.
The Wrap
widget automatically wraps the Chip
widgets onto a new row if they exceed the width of the screen. This allows us to create a flexible layout that adapts to different screen sizes and orientations.
Overall, the Wrap
widget is a useful tool for creating dynamic layouts that can accommodate a varying number of widgets.
Conclusion.
Each layout widget in Flutter provides a set of properties just like widgets discussed in previous blog, that you can use to customize its behavior and appearance. These properties allow you to control things like the alignment of child widgets, the spacing between widgets, and the padding around widgets.
For example, the alignment
property allows you to specify how child widgets should be aligned within a layout widget. You can align child widgets to the left, right, top, bottom, center, or anywhere in between. This is useful when you want to create a layout where child widgets are aligned in a specific way, such as a list of items where the item text is aligned to the left and the item price is aligned to the right.
The padding
property allows you to add padding around child widgets, which is useful when you want to add some extra space around a widget. For example, you might want to add some padding around an image widget to make it stand out more and give it some breathing room.
The spacing
property is another useful property that allows you to control the amount of space between child widgets. This is useful when you want to create a layout where child widgets are spaced apart in a specific way, such as a grid of images where there is some space between each image.
By using different combinations of layout widgets and properties, you can create complex and responsive user interfaces in Flutter. For example, you might use a Row layout widget to arrange child widgets horizontally, and then use the alignment
and spacing
properties to align the child widgets and add some space between them. Or you might use a Wrap layout widget to create a wrapped layout of child widgets, and then use the padding
property to add some extra space around each child widget.
In summary, the properties of layout widgets in Flutter allow you to customize the behavior and appearance of your user interfaces in a variety of ways. By using different combinations of layout widgets and properties, you can create flexible, responsive, and visually appealing layouts that meet the needs of your users.
Website: https://www.etechviral.com
LinkedIn eTechViral: https://www.linkedin.com/company/etechviral/?viewAsMember=true
YouTube: https://www.youtube.com/channel/UCO6gMNHYhRqyzbskNh4gG_A
eTechViral Instagram: https://www.instagram.com/etechviral/