Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically or horizontally center a Widget in a Stack?

The Center widget centers a widget horizontally and vertically in the center of a Stack. How can one center a widget ONLY horizontally or vertically?

Centering a widget in a Stack is possible with a Row or Column widget. Can it be done without using those widgets?

Conditions:

  • Cannot use Row / Column.
  • The size of the Stack is not known before layout (cannot use hardcoded Positioned values).

Center widget:

Centered Widget in Stack

Container(
      decoration:
          BoxDecoration(border: Border.all(color: Colors.black87, width: 10)),
      child: Stack(
        children: [
          Center(
            child: Container(color: Colors.amber, width: 50, height: 50),
          ),
        ],
      ),
    );

Center horizontally w/Row:

Horizontally centered widget in Stack

Row(mainAxisAlignment: MainAxisAlignment.center)

Center vertically w/Column:

Vertically centered Widget in Stack

Column(mainAxisAlignment: MainAxisAlignment.center)

The alignment above is the desired result. Can those results be achieved with other Flutter widgets?

like image 227
Ray Li Avatar asked Jul 17 '20 18:07

Ray Li


People also ask

How do I center widgets in stack flutter?

Using Stack's alignment property, you can align all the widgets in one direction. For example, all widgets in the center, all widgets in the bottom right, and so on. To position the widget in the Stack, simply add the alignment property and give it the alignment of your choice.

How do you vertically center a 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.

How do you horizontally center a widget in flutter?

Flutter – Center Align Horizontally in Column To center align contents of Column widget horizontally, set crossAxisAlignment attribute with CrossAxisAlignment.

How do I center a widget in ListView?

The solution is to place a Container as the only children in the ListView , and give it a minimum height equal to the available space for the height (Use LayoutBuilder to measure the maximum height for the widget). Then as the Container 's child, you can either place a Center or Column (with MainAxisAlignment.


1 Answers

Use Align Widget

 Align(
        alignment: Alignment.topCenter, // This will horizontally center from the top
        child: Container(
          decoration: BoxDecoration(
              border: Border.all(color: Colors.black87, width: 10)),
          child: Stack(
            children: [
              Container(color: Colors.amber, width: 50, height: 50),
            ],
          ),
        ),
      )

1. For Top Center use alignment: Alignment.topCenter

enter image description here

2. For Center Left use alignment: Alignment.centerLeft

enter image description here

3. For Center right use alignment: Alignment.centerRight

enter image description here

3. For Bottom center use alignment: Alignment.bottomCenter

enter image description here

like image 115
Jitesh Mohite Avatar answered Nov 15 '22 07:11

Jitesh Mohite