Search code examples
flutterdartalignment

How to align a Widget within a Column?


I want to align my button to the right. Right now I am accomplishing it with a Row that has mainAxisAlignment set toMainAxisAlignment.end like in the example below. However that seems not a good way because it is a lot of code and I don't think that is the way of doing it.

I already tried the Align widget, but that is not working. I guess because it is not expanding the inner widget and therefore the Alignment.centerRight is doing nothing.

The code I use right now:

new Row(
  crossAxisAlignment: CrossAxisAlignment.center,
  mainAxisAlignment: MainAxisAlignment.end,
  children: <Widget>[
    new FlatButton(
          onPressed: _navigateToPreparationStepsWidgetClicked,
          child: new Row(
            children: <Widget>[
              new Text(
                'View Preparation Steps'.toUpperCase(),
                style: new TextStyle(
                    fontWeight: FontWeight.bold),
              ),
              new Icon(Icons.keyboard_arrow_right)
            ],
          ))
  ],
)

What is the proper way to align e.g. a Button within e.g. a Column?


Solution

  • On closer inspection, it's because your button has a row in it. A row by default expands to take up as much space as possible it seems. So when you align your button to the right, it is doing it but the button itself takes up the entire row. If you changed the colour you'd see that more easily. The simple answer is to set mainAxisSize to MainAxisSize.min on the row in your button.

    This works just as expected for me:

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(),
            body: Column(
              children: <Widget>[
                FlatButton(child: Text("Default"), color: Colors.blue, onPressed: () {}),
                Align(
                  child: FlatButton(child: Text("Left"), color: Colors.red, onPressed: () {}),
                  alignment: Alignment.centerLeft,
                ),
                Center(child: FlatButton(child: Text("Centered"), color: Colors.orange, onPressed: () {})),
                Align(
                  child: FlatButton(child: Text("Right"), color: Colors.green, onPressed: () {}),
                  alignment: Alignment.centerRight,
                ),
                Align(
                  child: new FlatButton(
                    onPressed: () {},
                    child: new Row(
                      children: <Widget>[
                        new Text(
                          'View Preparation Steps'.toUpperCase(),
                          style: new TextStyle(fontWeight: FontWeight.bold),
                        ),
                        new Icon(Icons.keyboard_arrow_right)
                      ],
                      mainAxisSize: MainAxisSize.min,
                    ),
                  ),
                  alignment: Alignment.centerRight,
                ),
              ],
            ),
          ),
        );
      }
    }
    

    Which results in this:

    Screenshot of column with aligned items