6

I'm trying to align my more options button to the top right corner in in my column, according to the this SO answer.

How to align single widgets in Flutter?

Here's my code.

return Card(
      color: Colors.blueAccent,
      child: Container(
        height: 100,
        width: 350,
        child: Column(
          children: <Widget>[
            Text(
              'Day ${widget._dayNumber}',
              style: TextStyle(
                color: Colors.white,
              ),
            ),
            Align(alignment: Alignment.topRight,
             child: IconButton(onPressed: () {}, icon: Icon(Icons.more_vert),)),

          ],
        ),
      ),
    );

Text first, then align

And if I change the order of align and text, this happens.

align first, then text

I want to display my button on the top right corner while keeping Text widget on the center top, but align widget seems to take whole line(row).

Is there a way to do correctly achieve that, or do I need to wrap them both in a row?

mirkancal
  • 4,762
  • 7
  • 37
  • 75

2 Answers2

13

I've used Stack and Positioned widget to achieve that effect.

Final

Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Card(
          color: Colors.blueAccent,
          child: Container(
            height: 100,
            width: 350,
            child: Column(
              children: <Widget>[
                Text(
                  'Day ${widget._dayNumber}',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
        Positioned(
          top: 0,
          right: 0,
          child: IconButton(
            onPressed: () {},
            icon: Icon(Icons.more_vert),
          ),
        ),
      ],
    );
  }
mirkancal
  • 4,762
  • 7
  • 37
  • 75
  • Another option for a widget instead of Positioned with two properties set to `0` is to use an `Align` and set the `alignment` property to `topRight` – Jonas Dec 06 '19 at 18:05
6

You need to wrap with Row if you want to make them in the same Column enter image description here

  return Card(
    color: Colors.blueAccent,
    child: Container(
      height: 100,
      width: 350,
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Container(),
              Text(
                'Day 1',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
              IconButton(
                onPressed: () {},
                icon: Icon(Icons.more_vert),
              ),
            ],
          ),
        ],
      ),
    ),
  );
cipli onat
  • 1,943
  • 1
  • 11
  • 26