247

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

jeroen-meijer
  • 2,664
  • 2
  • 13
  • 16

13 Answers13

522

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,
        ),
      ),
jeroen-meijer
  • 2,664
  • 2
  • 13
  • 16
Shelly Pritchard
  • 10,777
  • 4
  • 18
  • 17
  • 2
    @Shelly In some cases it won't work you need to add heightFactor: property also e.g : Center( heightFactor: 2.3, child: Text('SELFIE',textAlign: TextAlign.center, style: TextStyle(fontSize: 18.0, color: Colors.black, fontWeight: FontWeight.bold, ) ), ), – Sanjeev Sangral May 24 '19 at 07:37
  • I took a look and agree. Changed this to be the correct answer since it centers horizontally and vertically. – jeroen-meijer Sep 20 '19 at 12:08
  • 2
    If you find that the text is still not vertically centred, and you have set the height in TextStyle, remember to set leadingDistribution: TextLeadingDistribution.even as well in TextStyle. – Victor Kwok Dec 17 '21 at 08:19
112

You can use TextAlign property of Text constructor.

Text("text", textAlign: TextAlign.center,)
nbro
  • 15,395
  • 32
  • 113
  • 196
Tree
  • 29,135
  • 24
  • 78
  • 98
  • 61
    I'm not sure why this was marked as the answer, as this only centers text horizontally, but the question asks for horizontally **and** vertically. – Herohtar Feb 21 '19 at 03:06
  • 4
    Yes this should be removed, it doesn't answer the question. – Hasen Jul 07 '19 at 10:59
87

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.

Mike
  • 1,349
  • 1
  • 17
  • 23
  • 3
    Much better answer is this one. – Nikola Jovic Jan 29 '20 at 08:38
  • this should be the marked answer cause the question is about how to center a text in horizontal and vertical. Thank you! – aligur Feb 01 '20 at 13:15
  • This is the answer guys, just wondering why my text was not aligning with a bigger font size, Turns out that center it's weird with text and there is a breakpoint in wich will ignore the text size. Align seems not to have this issue... love u <3 – RegularGuy Aug 27 '20 at 15:25
  • 1
    You may need to set `TextStyle( leadingDistribution: TextLeadingDistribution.even)` too for large fonts to get them centered properly vertically. – dan-gph Sep 15 '22 at 10:05
17

Wrap your text widget with Align and set alignment to Alignment.center.

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

This produced the best result for me.

Andreas Violaris
  • 2,465
  • 5
  • 13
  • 26
Paul Kitatta
  • 409
  • 1
  • 7
  • 12
11

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,
    ),
  ),
Samuel Quiroz
  • 191
  • 2
  • 5
  • 4
    Though you're technically correct, it is a syntax error to do it in the first way you described, so yes, but I think it's clear that that's not the way to do it since it won't even let you compile :p – jeroen-meijer Jun 08 '22 at 15:02
10

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

Text(
  'Hi there',
   textAlign: TextAlign.center,                          
 )
Saad
  • 539
  • 2
  • 19
Kasujja Muhammed
  • 422
  • 6
  • 11
7

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

Ray
  • 394
  • 3
  • 11
Ray Coder
  • 1,022
  • 3
  • 15
  • 30
6

Put the Text in a Center:

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

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 ‍

Ramesh R C
  • 644
  • 7
  • 17
3

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))
  ])
Golden Lion
  • 3,840
  • 2
  • 26
  • 35
1

you have to set your Text widget property to set textAlign.center.

center(
   child : Text(
  'Hello',
   textAlign: TextAlign.center))
Nidhi
  • 51
  • 3
1

Wrap your Text widget inside the Align widget and add Alignment.center.

Align(
  alignment: Alignment.center,
  child: Text(
    'Some Text',
    style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
  ),
)
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
-2

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,
              ),),
            ),
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33