Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to align widget to the topRight of column?

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?

like image 213
mirkancal Avatar asked Mar 20 '19 10:03

mirkancal


People also ask

How do you align widgets at the end of a row of Flutter?

you can add Spacer(), above the widget you want to put at the end. Save this answer.


1 Answers

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),
          ),
        ),
      ],
    );
  }
like image 84
mirkancal Avatar answered Sep 25 '22 18:09

mirkancal