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 ?
Flutter – Center Align Text in Text Widget To center align the text in a Text widget, provide textAlign property with value TextAlign. center .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With