Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CrossAxisAlignment does not center column widgets

I am new to flutter and I have been trying to center my column widgets just to grasp an understanding of the layouts but the CrossAxisAlignment property of the Column doesn't seem to work.

I have tried enclosing the column into several other widgets like container but the solutions I have found so far just overwrite the core functionality of CrossAxisAlignment property. If I wrap the entire the Column in a Center widget then it is centered on the horizontal axis by default and CrossAxisAlignment property wouldn't work.

The only solution I found was to wrap the column in a container and setting its width property to take up entire width of the screen but that seems like brute forcing the layout instead of using the dynamic behaviour of Flutter.

Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0.0,
        leading: Icon(
          Icons.arrow_back_ios,
          color: Colors.black,
        ),
      ),
      body: Container(
      color: Colors.grey[500],
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            color: Colors.orange,
            child: FlutterLogo(
              size: 60.0,
            ),
          ),
          Container(
            color: Colors.blue,
            child: FlutterLogo(
              size: 60.0,
            ),
          ),
          Container(
            color: Colors.purple,
            child: FlutterLogo(
              size: 60.0,
            ),
          ),
        ],
      ),
    )
);

Output:

Column uncentered

Only by hard setting the width property does it center properly

Container(
   width: MediaQuery.of(context).size.width,

Output:

Column Centered

My question is is there a better way to use CrossAxisAlignment property without manually fixing the width? And in what use cases does it actually work without any wrapper code?

like image 268
Parichit Avatar asked Aug 02 '19 08:08

Parichit


People also ask

How do you center a column widget in flutter?

In Flutter, to vertically center a child widget inside a Container, you can wrap the child widget with Column and add mainAxisAlignment: MainAxisAlignment. center to it.

What is MainAxisAlignment in flutter?

Main Axis is vertical and the Cross Axis is horizontal. MainAxisAlignment aligns its children vertically and CrossAxisAlignment aligns horizontally in that Column.

What is the difference between crossaxisalignment in row and column?

The cross axis to a Row's main axis is vertical. So using crossAxisAlignment in a Row lets you define, how its children are aligned vertically. In a Column, it's the opposite. The children of a column are laid out vertically, from top to bottom (per default).

How do you layout a cross axis alignment?

Layout each child a null or zero flex factor (e.g., those that are not Expanded) with unbounded vertical constraints and the incoming horizontal constraints. If the crossAxisAlignment is CrossAxisAlignment.stretch, instead use tight horizontal constraints that match the incoming max width.

How to align content as per your choice using alignment properties?

Alignment Properties: We can align content as per our choice by using mainAxisAlignment and crossAxisAlignment. Row’s mainAxis is horizontal and cross Axis to Row’s main Axis is vertical. We can align children horizontally using MainAxisAlignment and vertically using CrossAxisAlignment in that row.

What is the difference between crossaxisalignment and mainaxissize?

In the following example, the crossAxisAlignment is set to CrossAxisAlignment.start, so that the children are left-aligned. The mainAxisSize is set to MainAxisSize.min, so that the column shrinks to fit the children.


2 Answers

The thing is: the CrossAxisAlignment works, but your column is only as wide as it shows on the image. This way it looks like nothing is happening.

To get your items in the center of the screen, you have to move your entire Column to the center. You can achieve this by wrapping your column inside an Align widget and setting alignment: Alignment.center.

like image 173
Noodles Avatar answered Sep 20 '22 18:09

Noodles


Add alignment

body: Container(
    alignment: Alignment.topCenter, ...
like image 40
Andrey Turkovsky Avatar answered Sep 20 '22 18:09

Andrey Turkovsky