The Column widget is a vertical layout widget in Flutter that allows you to display multiple widgets one after the other in a single column. It is ideal for scenarios where you want to display multiple widgets such as icons, text, or images in a column, and you want to control their alignment and positioning. You can set various parameters for the Column widget, including the main axis alignment, cross axis alignment, and the size of the column in the main axis.
The Column widget takes in a list of child widgets as its input, which can be of any type. The children can be added to the Column widget either directly in the constructor or by calling the add() method of the children property. You can also set the vertical direction of the column to either from top-to-bottom or bottom-to-top using the verticalDirection property. Additionally, you can use the mainAxisAlignment and crossAxisAlignment properties to control the alignment of the children widgets.
“CrossAxisAlignment” & “MainAxisAlignment”
mainAxisAlignment
and crossAxisAlignment
are properties of the Row
and Column
widgets in Flutter that control the alignment of the child widgets in the main axis and the cross axis respectively.
The main axis is the axis that runs parallel to the direction of the row or column, while the cross axis runs perpendicular to the direction of the row or column.
mainAxisAlignment
is used to control the alignment of the child widgets in the main axis. You can set it to start
, end
, center
, spaceBetween
, spaceAround
, or spaceEvenly
. start
aligns the children to the start of the main axis, end
aligns them to the end of the main axis, center
aligns them to the center of the main axis, spaceBetween
spaces the children out evenly across the main axis with the first child at the start and the last child at the end, spaceAround
spaces the children out evenly across the main axis with space between each child, and spaceEvenly
spaces the children out evenly across the main axis with equal space between each child and at the start and end.
crossAxisAlignment
, on the other hand, is used to control the alignment of the child widgets in the cross axis. You can set it to start
, end
, center
, stretch
, or baseline
. start
aligns the children to the start of the cross axis, end
aligns them to the end of the cross axis, center
aligns them to the center of the cross axis, stretch
stretches the children to fill the available space in the cross axis, and baseline
aligns the children to a common baseline.
By using these properties, you can create flexible and responsive layouts in Flutter.
Examples
Here’s an example of how to create a Column widget in Flutter:
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('Widget 1'),
Text('Widget 2'),
Text('Widget 3'),
],
)
In this example, we have created a simple Column widget that contains three Text widgets. The mainAxisAlignment and crossAxisAlignment properties are set to center, which centers the children widgets both horizontally and vertically in the column.