Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter row place one item at start and other item in the middle

I have a card in which the first row of this card will have 2 items of text.

I would like to place an item at the very beginning and then the next item directly in the centre.

I have played around with MainAxisAlignment but nothing seems to cater for this scenario for example if I use MainAxisAlignment.spaceBetween then each item is placed at the end of the rows.

Is there an obvious way how to do this ?

Thanks

like image 913
drlobo Avatar asked Jan 23 '19 12:01

drlobo


3 Answers

You could use the Spacer widget to fill available space in the row:

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: SpacedRow(),
          ),
        ),
      ),
    );
  }
}

class SpacedRow extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Container(width: 50, height: 50, color: Colors.red),
        Spacer(),
        Container(width: 50, height: 50, color: Colors.yellow),
        Spacer(),
      ],
    );
  }
}

Produces:

Screenshot

like image 131
Jordan Davies Avatar answered Sep 28 '22 17:09

Jordan Davies


Though Jordan's answer achieved what the question mentioned, the yellow block does not look exactly center. I tried something like following

Stack(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Image.asset(
                  "images/toolbar_logo.webp",
                  width: 80,
                  height: 50,
                ),
              ],
            ),
            IconButton(
              icon: Icon(Icons.navigation),
              onPressed: () {},
              iconSize: 20,
            ),
          ],
        ),
like image 36
Pritish Avatar answered Sep 28 '22 17:09

Pritish


You can use Spacer(), Expanded(), Flexible(), MainAxisAlignment.spaceBetween but here you may need to provide padding. There are many ways to achieve that. If you can share the screenshot or code, we can post the code accordingly.

like image 33
CopsOnRoad Avatar answered Sep 28 '22 17:09

CopsOnRoad