In Column widget of flutter there are 6 different options of spacing is available. Basically these are the vertical alignment options of Column widget which can be accessed through mainAxisAlignment property of Column. mainAxisAlignment set the column children alignment in vertically manner because its default axis is vertical.
Vertical spacing & alignment properties in Column widget:
- MainAxisAlignment.start : Place all the Column children at the start and close to each other in main axis manner.
- MainAxisAlignment.end : Place the children at the end of layout screen in main axis manner.
- MainAxisAlignment.center : To place all the children at the vertically middle of screen.
- MainAxisAlignment.spaceAround : Place free space evenly between children of column widget.
- MainAxisAlignment.spaceBetween : Place free space between each element of Column widget. First and Last children of column will be touch without space to the top and bottom screen.
- MainAxisAlignment.spaceEvenly : Place free space to the children and first and last child would also get spacing from top and bottom side.
Flutter Set Vertical Spacing Between Column Children Widgets:
1. Creating column widget with MainAxisAlignment.start .
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
width: 250,
height: 50,
color: Colors.amber,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.green,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.brown,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.pink,
alignment: Alignment.center,
),
])
Screenshot:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 250,
height: 50,
color: Colors.amber,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.green,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.brown,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.pink,
alignment: Alignment.center,
),
])
Screenshot:
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
width: 250,
height: 50,
color: Colors.amber,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.green,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.brown,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.pink,
alignment: Alignment.center,
),
])
Screenshot:
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
width: 250,
height: 50,
color: Colors.amber,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.green,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.brown,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.pink,
alignment: Alignment.center,
),
])
Screenshot:
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
width: 250,
height: 50,
color: Colors.amber,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.green,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.brown,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.pink,
alignment: Alignment.center,
),
])
Screenshot:
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
width: 250,
height: 50,
color: Colors.amber,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.green,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.brown,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.pink,
alignment: Alignment.center,
),
])
Screenshot:
import 'package:flutter/material.dart';
void main() => runApp(const App());
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
width: 250,
height: 50,
color: Colors.amber,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.green,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.brown,
alignment: Alignment.center,
),
Container(
width: 250,
height: 50,
color: Colors.pink,
alignment: Alignment.center,
),
])))));
}
}
Comments
Post a Comment