Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I align one widget to the top of the screen and center another widget to the middle of the screen in Flutter?

Within the same body I'm trying to align one widget to the top of the screen and another to the center of the screen. This is proving difficult.

Here is my code:

body: Column(children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              color: Colors.redAccent,
              width: screenWidth,
              height: 50.0,
              child: Center(
                child: Text(
                  "Hello World!",
                  style: TextStyle(
                    fontSize: 18.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              color: Colors.blueAccent,
              width: screenWidth,
              height: 50.0,
              child: Center(
                child: Text(
                  "Thanks for the help!",
                  style: TextStyle(
                    fontSize: 18.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ],
        ),
      ]),

When this code is run in my Xcode simulator this is the result:

https://i.imgur.com/JtPKyq0.png

So to be clear this result is wrong because I want the blue container to be at the center of the screen, not the top.

Thanks in advance for any help!

like image 778
hello_friend Avatar asked Feb 17 '19 23:02

hello_friend


People also ask

How do you align widgets in Flutter?

To set the alignment of a widget in Flutter, you can wrap it as the child of an Align widget and pass the alignment argument to adjust the position.

How do you align a widget in the center of the screen Flutter?

Use Column If we wrap a Column within a Container with max width and height, we can set Column's children to center aligned by applying mainAxisAlignment: MainAxisAlignment. center and crossAxisAlignment: CrossAxisAlignment. center .


1 Answers

One option is to use an Expanded widget. This widget will expand fill all the space available in the parent, and then you center the child inside it. Note that this only works inside Flex widgets, like Row, Column etc.

In your case, I also recommend removing the screenWidth variable for the row width, and use the Expanded widget to make the container fill the screen horizontally.

This is the final code:

body: Column(children: <Widget>[
    Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Expanded( //makes the red row full width
          child: Container(
            color: Colors.redAccent,
            height: 50.0,
            child: Center(
              child: Text(
                "Hello World!",
                style: TextStyle(
                  fontSize: 18.0,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
      ],
    ),
    // This expands the row element vertically because it's inside a column
    Expanded( 
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          // This makes the blue container full width.
          Expanded(
            child: Container(
              color: Colors.blueAccent,
              height: 50.0,
              child: Center(
                child: Text(
                  "Thanks for the help!",
                  style: TextStyle(
                    fontSize: 18.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    ),
  ]
),
like image 125
George Avatar answered Oct 27 '22 19:10

George