Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center only one element in a row of 2 elements in flutter

In my layout I have two Widgets in a row, one text and the an icon.

As shown below, assume that * refers to the icon, and using "-" to represent the row:

----------------------------            Text          *   ---------------------------- 

How do I make my text centered in the entire row, and the icon to the right end ?

like image 299
Bajji Avatar asked Jul 30 '18 04:07

Bajji


People also ask

How do you align Text in the center of a row in Flutter?

Flutter – Center Align Text in Text Widget To center align the text in a Text widget, provide textAlign property with value TextAlign. center .

How do I center a single widget in column Flutter?

How to Align Single Widgets In Flutter ?? To align a child widget within its parent you use the Align widget. If you know how to use the Center widget then you are on the right track because Center is just a special case of Align. Wrap the widget you wish to align with the Align widget and set its alignment property.

How do you center horizontally in Flutter?

Creating Widgets Here, you have a Scaffold with Row widget which you will use to center a widget. There is a property i.e mainAxisAlignment and this property takes the MainAxisAlignment function. To center a widget you can use MainAxisAlignment. center method.


1 Answers

The main thing you need to notice is that if you don't want to use Stack you should provide same width-consuming widgets both on left and right.

I would do this with either Expanded and Padding

Row(   children: <Widget>[     Expanded(       child: Padding(         padding: const EdgeInsets.only(left: 32.0),         child: Text(           "Text",           textAlign: TextAlign.center,         ),       ),     ),     Icon(Icons.add, size: 32.0,)   ], ) 

or with Row's mainAxisAlignment and a SizedBox

Row(   mainAxisAlignment: MainAxisAlignment.spaceBetween,   children: <Widget>[     const SizedBox(width: 32.0),     Text("Text"),     Icon(Icons.add, size: 32.0)   ], ) 

or with Expanded and a SizedBox on the left instead of the padding :). The padding or extra container on the left is so that the textAlign will truly center the text in the row taking into account the space taken up by the Icon.

like image 52
Marcin Szałek Avatar answered Oct 28 '22 02:10

Marcin Szałek