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),
),
),
),
),
);
3
Answers
you can use
Center
widget to align it centerand you text is not center because
fontSize
is too large so decrease itTry this:
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 ofTextStyle
:If you set the
height
to1.1
for example, you can see it now looks centered:References: