I'd like to know how to center the contents a Text widget vertically and horizontally in Flutter.
I only know how to center the widget itself using Center(child: Text("test"))
but not the content itself, it's by default left aligned. In Android, I believe the property of a TextView that achieves this is called gravity
.
Example of what I want:
Text alignment center property setting only horizontal alignment.
I used below code to set text vertically and horizontally center.
Code:
You can use
TextAlign
property ofText
constructor.This produced the best result for me.
Put the Text in a Center:
I think a more flexible option would be to wrap the
Text()
withAlign()
like so:Using
Center()
seems to ignoreTextAlign
entirely on the Text widget. It will not alignTextAlign.left
orTextAlign.right
if you try, it will remain in the center.Text element inside Center of SizedBox work much better way, below Sample code
Enjoy coding