How do I center text vertically and horizontally in Flutter?

302,922

Solution 1

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

You can use TextAlign property of Text constructor.

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

Solution 3

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

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

This produced the best result for me.

Solution 5

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

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

Related videos on Youtube

jeroen-meijer
Author by

jeroen-meijer

Well, hello there! I’m Jay, a Dutch software developer passionate about Flutter, Dart and TypeScript! I’m an admin and maintainer for the Flutter Community organization and host of the live programming series Generic Typing on Twitch and YouTube. When I’m not working for the community, I spend my time doing freelance Flutter work, grabbing beers with friends or making music.

Updated on July 08, 2022

Comments

  • jeroen-meijer
    jeroen-meijer almost 2 years

    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

  • Herohtar
    Herohtar about 5 years
    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.
  • Sanjeev Sangral
    Sanjeev Sangral almost 5 years
    @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, ) ), ),
  • Hasen
    Hasen almost 5 years
    Yes this should be removed, it doesn't answer the question.
  • jeroen-meijer
    jeroen-meijer over 4 years
    I took a look and agree. Changed this to be the correct answer since it centers horizontally and vertically.
  • Nikola Jovic
    Nikola Jovic over 4 years
    Much better answer is this one.
  • aligur
    aligur about 4 years
    this should be the marked answer cause the question is about how to center a text in horizontal and vertical. Thank you!
  • ValdaXD
    ValdaXD over 3 years
    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
  • Benjamin Charais
    Benjamin Charais almost 3 years
    This absolutely made my day. I kept trying to use the IDE wrap didn't think of the context menu. Thank you!
  • Ray Coder
    Ray Coder almost 3 years
    Your welcome. Hope you can save your time!
  • stan
    stan over 2 years
    Great hint in general! That makes life a lot easier
  • Victor Kwok
    Victor Kwok over 2 years
    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.
  • jeroen-meijer
    jeroen-meijer almost 2 years
    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