Column Widget and Attributes.

Column Widget and Attributes.

ยท

3 min read

Let's delve into the Column widget and provide more explanation along with a simple example.

The Column widget in Flutter is a layout widget that displays its children in a vertical sequence. It arranges its children from top to bottom. Each child can be any widget, and they will be drawn in the order they appear in the children property.

The Column widget in Flutter has several attributes (also known as properties) that you can use to customize its behavior and appearance. Here are some of the key attributes:

  1. children: A list of widgets to be displayed in the column, arranged in the order they appear in the list.

     Column(
       children: [
         Text('First Child'),
         Text('Second Child'),
         // More children...
       ],
     )
    
  2. mainAxisAlignment: Defines how the children should be placed along the main axis (vertically, in the case of a Column). Possible values include:

    • start: Align children at the top.

    • end: Align children at the bottom.

    • center: Center children in the middle.

    • spaceBetween: Space children evenly along the main axis with space between them.

    • spaceAround: Space children evenly along the main axis with space around them.

    • spaceEvenly: Space children evenly along the main axis with equal space on both ends.

    Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('Centered Child'),
        // More children...
      ],
    )
  1. crossAxisAlignment: Defines how the children should be placed along the cross axis (horizontally, in the case of a Column). Possible values include:

    • start: Align children at the start of the cross axis.

    • end: Align children at the end of the cross axis.

    • center: Center children along the cross axis.

    • stretch: Stretch children to fill the cross axis.

    Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text('Centered Child'),
        // More children...
      ],
    )
  1. mainAxisSize: Defines the size of the column along its main axis. Possible values are MainAxisSize.min (shrinks to fit its children) and MainAxisSize.max (takes up as much space as possible along the main axis).

     Column(
       mainAxisSize: MainAxisSize.max,
       children: [
         // Children...
       ],
     )
    
  2. verticalDirection: Defines the order in which the children are placed along the main axis. Possible values include VerticalDirection.down (top to bottom) and VerticalDirection.up (bottom to top).

     Column(
       verticalDirection: VerticalDirection.down,
       children: [
         // Children...
       ],
     )
    

These attributes allow you to control the layout and appearance of a Column widget. You can combine and customize them according to your UI design requirements.

Here's an example that demonstrates the use of Column with various widgets:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Column Widget Example'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text('First Child', style: TextStyle(fontSize: 20)),
            Container(
              color: Colors.blue,
              height: 50,
              width: 150,
              child: Center(child: Text('Second Child', style: TextStyle(color: Colors.white))),
            ),
            FlutterLogo(size: 100),
          ],
        ),
      ),
    );
  }
}

In this example:

  • The first child is a simple text widget.

  • The second child is a container with a blue background, containing centered text.

  • The third child is a Flutter logo.

You can customize the appearance and arrangement of your widgets within the Column according to your layout requirements. Feel free to experiment with different properties to achieve the desired visual result.

Did you find this article valuable?

Support Vinit Mepani by becoming a sponsor. Any amount is appreciated!

ย