Flutter align two items on extremes - one on the left and one on the right

FlutterFlutter Layout

Flutter Problem Overview


I am trying to align two items at extremes one on the left and one on the right. I have one row that is aligned to the left and then a child of that row is aligned to the right. However it seems the child row is picking up the alignment property from its parent. This is my code

var SettingsRow = new Row(
			mainAxisAlignment: MainAxisAlignment.end,
			crossAxisAlignment: CrossAxisAlignment.center,
			mainAxisSize: MainAxisSize.max,
			children: <Widget>[
				Text("Right",softWrap: true,),
			],
		);

		var nameRow = new Row(
			mainAxisAlignment: MainAxisAlignment.start,
			crossAxisAlignment: CrossAxisAlignment.center,
			mainAxisSize: MainAxisSize.max,
			children: <Widget>[
				Text("Left"),
				SettingsRow,
			],
		);

as a result I get something like this

Left Right

What I would like is

Left      Right

Also there is enough space on the Row. My question is why is the child Row not exhibiting its MainAxisAlignment.end property ?

Flutter Solutions


Solution 1 - Flutter

Use a single Row instead, with mainAxisAlignment: MainAxisAlignment.spaceBetween.

new Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    new Text("left"),
    new Text("right")
  ]
);

Or you can use Expanded

new Row(
  children: [
    new Text("left"),
    new Expanded(
      child: settingsRow,
    ),
  ],
);

Solution 2 - Flutter

A simple solution would be to use Spacer() between the two widgets

Row(
  children: [
    Text("left"),
    Spacer(),
    Text("right")
  ]
);

Solution 3 - Flutter

You can do it in many ways.

Using mainAxisAlignment: MainAxisAlignment.spaceBetween

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: <Widget>[
    FlutterLogo(),
    FlutterLogo(),
  ],
);

Using Spacer

Row(
  children: <Widget>[
    FlutterLogo(),
    Spacer(),
    FlutterLogo(),
  ],
);

Using Expanded

Row(
  children: <Widget>[
    FlutterLogo(),
    Expanded(child: SizedBox()),
    FlutterLogo(),
  ],
);

Using Flexible

Row(
  children: <Widget>[
    FlutterLogo(),
    Flexible(fit: FlexFit.tight, child: SizedBox()),
    FlutterLogo(),
  ],
);

Output:

enter image description here

Solution 4 - Flutter

Yes CopsOnRoad is right, through stack you can align one on right and other at center.

Stack(
    children: [
      Align(
        alignment: Alignment.centerRight,
        child: Text("right"),
      ),
      Align(
        alignment: Alignment.center,
        child: Text("center"),
      )
    ],
  )

Solution 5 - Flutter

use the line of code mainAxisAlignment: MainAxisAlignment.spaceBetween, in a Row. That will allow The widgets to be on two extremes. However if you are working on TextField, use decoration class and the attribute prefix*** and suffix*** instead

Solution 6 - Flutter

Using Flexing Widget

Row(
            children: [
              Text(
                '1',
                style: TextStyle(
                    fontSize: 18, color: Colors.black),
              ),
              Flexible(fit: FlexFit.tight, child: SizedBox()),
              Text(
                '10',
                style: TextStyle(
                    fontSize: 18, color: Colors.black),
              ),
            ],
          ),

Solution 7 - Flutter


I had multiple entries in my row, and the accepted answer didn't work for me.
I needed to wrap the final entry in an Align.


I had :

Row( 
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    ...allMyLeftAlignedChildren,
    Expanded( 
      child: myRightAlignedChild, 

And I needed :

Row( 
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    ...allMyLeftAlignedChildren,
    Expanded( 
      child: Align(                       // <---  these 2 lines fixed it 
        alignment: Alignment.centerRight, // <---  these 2 lines fixed it
        child: myRightAlignedChild, 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMistyDView Question on Stackoverflow
Solution 1 - FlutterRémi RousseletView Answer on Stackoverflow
Solution 2 - FlutterPritishView Answer on Stackoverflow
Solution 3 - FlutterCopsOnRoadView Answer on Stackoverflow
Solution 4 - Flutterpoonam kalraView Answer on Stackoverflow
Solution 5 - FlutterOyekunle OyegunleView Answer on Stackoverflow
Solution 6 - FlutterQuick learnerView Answer on Stackoverflow
Solution 7 - FlutterkrisView Answer on Stackoverflow