Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to vertically and horizontally center a text within a container in flutter

I want to vertically and horizontally center a text within a white container in flutter.

Code I used to generate the following example but it is not vertically centered. Any idea how to?

return Center(
  child: Container(
    color: Colors.white,
    width: 105,
    height: 105,
    child: Align(
      alignment: Alignment.center,
      child: Container(
        color: Colors.green,
        child: Text(
          '1',
          style: TextStyle(fontSize: 100),
        ),
      ),
    ),
  ),
);

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your code, the Text widget is already centered vertically, but the text itself has some line height that makes it looks like there is some space on the top.

From the documentation of height property of TextStyle:

Text line height


If you set the height to 1.1 for example, you can see it now looks centered:

Text(
  '1',
  style: TextStyle(
    fontSize: 100,
    height: 1.1,
  ),
)

enter image description here


References:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading