Column in Flutter

18-Sep-2024

Learn how to arrange widgets vertically using the Column widget in Flutter

In Flutter, a column is a widget that displays its children in a vertical array.
Widgets can be arranged vertically and stacked.
Each child of a column can be any Flutter widget, allowing you to create complex layouts.


There Are Some Common Attributes for Column

mainAxisAlignment
spaceAround, center, spaceBetween, spaceEvenly
crossAxisAlignment
center, stretch, start
mainAxisSize
max, min



Column(
mainAxisAlignment: MainAxisAlignment.center,//you can use spaceAround,center,spaceBetween,spaceEvenly
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// Widget Here
// Widget Here
// Widget Here
],
);


Here's a simple example of using a Column in Flutter:

File open lib->main.dart file −



import 'package:flutter/material.dart';

void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Column Example'),
),
body: MyApp(),
),
)
);
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,//you can use spaceAround,center,spaceBetween,spaceEvenly
crossAxisAlignment: CrossAxisAlignment.center,//you can use center, stretch, start
children: [
Container(
height: 100,
width: 100,
color: Colors.blue,
alignment: Alignment.center,
child: Text("Text 1"),
),
Container(
height: 100,
width: 100,
color: Colors.red,
alignment: Alignment.center,
child: Text("Text 2"),
),
Container(
height: 100,
width: 100,
color: Colors.amber,
alignment: Alignment.center,
child: Text("Text 3"),
)
],
)
);

}
}





1/ Column is the main widget used to vertically arrange child elements.
2/ mainAxisAlignment is used to align the child along the main axis (in this case the vertical axis).
3/ crossAxisAlignment is used to align the child along the cross axis (in this case the horizontal axis).

 The Children property is a list of widgets  to display in the column.

Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.


Output:


Comments