How do I center text vertically and horizontally in Flutter?

User InterfaceTextWidgetAlignmentFlutter

User Interface Problem Overview


I'd like to know how to center the contents of 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. By default, it's aligned to the left. In Android, I believe the property of a TextView that achieves this is called gravity.

Example of what I want:

centered text example

User Interface Solutions


Solution 1 - User Interface

Text alignment center property setting only horizontal alignment.

enter image description here

I used below code to set text vertically and horizontally center.

enter image description here

Code:

      child: Center(
        child: Text(
          "Hello World",
          textAlign: TextAlign.center,
        ),
      ),

Solution 2 - User Interface

You can use TextAlign property of Text constructor.

Text("text", textAlign: TextAlign.center,)

Solution 3 - User Interface

I think a more flexible option would be to wrap the Text() with Align() like so:

Align(
  alignment: Alignment.center, // Align however you like (i.e .centerRight, centerLeft)
  child: Text("My Text"),
),
              

Using Center() seems to ignore TextAlign entirely on the Text widget. It will not align TextAlign.left or TextAlign.right if you try, it will remain in the center.

Solution 4 - User Interface

                       child: Align(
                          alignment: Alignment.center,
                          child: Text(
                            'Text here',
                            textAlign: TextAlign.center,                          
                          ),
                        ),

This produced the best result for me.

Solution 5 - User Interface

U need to use textAlign property on the Text widget. It produced the best results for me

Text(
  'Hi there',
   textAlign: TextAlign.center,                          
 )

Solution 6 - User Interface

If you are a intellij IDE user, you can use shortcut key Alt+Enter and then choose Wrap with Center and then add textAlign: TextAlign.center

Solution 7 - User Interface

THE TEXT TITLE MUST BE ALWAYS ON TOP.

wrong way:

  child: Center(
    child: Text(
      textAlign: TextAlign.center,
      "Hello World",
    ),
  ),

right way:

  child: Center(
    child: Text(
      "Hello World",
      textAlign: TextAlign.center,
    ),
  ),

Solution 8 - User Interface

Put the Text in a Center:

Container(
      height: 45,
      color: Colors.black,
      child: Center(
        child: Text(
            'test',
            style: TextStyle(color: Colors.white),
        ),
      ),
    );

Solution 9 - User Interface

Text element inside Center of SizedBox work much better way, below Sample code

Widget build(BuildContext context) {
    return RawMaterialButton(
      fillColor: Colors.green,
      splashColor: Colors.greenAccent,
      shape: new CircleBorder(),
      child: Padding(
        padding: EdgeInsets.all(10.0),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            SizedBox(
              width: 100.0,
              height: 100.0,
              child: Center(
                child: Text(
                widget.buttonText,
                maxLines: 1,
                style: TextStyle(color: Colors.white)
              ),
              )
          )]
        ),
    ),
  onPressed: widget.onPressed
);
}

Enjoy coding ‍

Solution 10 - User Interface

Overview: I used the Flex widget to center text on my page using the MainAxisAlignment.center along the horizontal axis. I use the container padding to create a margin space around my text.

  Flex(
            direction: Axis.horizontal,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
                Container(
                    padding: EdgeInsets.all(20),
                    child:
                        Text("No Records found", style: NoRecordFoundStyle))
  ])

Solution 11 - User Interface

To center items in a container user mainAxisAlignment: MainAxisAlignment.center,

     Container(
         child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
                children: const [
                   Align(
                        alignment: Alignment.center,
                              child: Text('Student Name',
                                  style: TextStyle(
                                      fontSize: 16,
                                      fontWeight: FontWeight.bold),
                                textAlign: TextAlign.left),
                         ),                
                     ]),
                )

Solution 12 - User Interface

maybe u want to provide the same width and height for 2 container

Container(
            width: size.width * 0.30, height: size.height * 0.4,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(6)
            ),
            child: Center(
              child: Text(categoryName, textAlign: TextAlign.center, style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 17,
                color: Colors.white,
              ),),
            ),

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
Questionjeroen-meijerView Question on Stackoverflow
Solution 1 - User InterfaceShelly PritchardView Answer on Stackoverflow
Solution 2 - User InterfaceTreeView Answer on Stackoverflow
Solution 3 - User InterfaceMikeView Answer on Stackoverflow
Solution 4 - User InterfacePaul KitattaView Answer on Stackoverflow
Solution 5 - User InterfaceKasujja MuhammedView Answer on Stackoverflow
Solution 6 - User InterfaceRay CoderView Answer on Stackoverflow
Solution 7 - User InterfaceSamuel QuirozView Answer on Stackoverflow
Solution 8 - User InterfaceMinghangView Answer on Stackoverflow
Solution 9 - User InterfaceRamesh R CView Answer on Stackoverflow
Solution 10 - User InterfaceGolden LionView Answer on Stackoverflow
Solution 11 - User InterfaceKiprutoView Answer on Stackoverflow
Solution 12 - User Interfacesomeone_that_needHelpView Answer on Stackoverflow