I have a Wrap
layout with some Container
children. Each container has a child, Text
The Container
has a fixed height but a flexible width.
How can I vertically align the Text
inside the Container
? I tried to use alignment: Alignment.centerLeft
, but then the Container's width spans the whole parent.
I can not set the width of the Container
since I don't know what width the text will be.
Wrap(
spacing: 3.0, // gap between adjacent chips
runSpacing: 3.0, // gap between lines
children: <Widget>[
new Container(
height: 30,
alignment: Alignment.centerLeft,
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(15),
),
child: Text(
'hallo',
style: TextStyle(background: _paint,),
),
),
...
This is how it should look like - but the red text should be vertically centered:
The CSS just sizes the div, vertically center aligns the span by setting the div's line-height equal to its height, and makes the span an inline-block with vertical-align: middle. Then it sets the line-height back to normal for the span, so its contents will flow naturally inside the block.
To just center the text inside an element, use text-align: center; This text is centered.
Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .
I don't think we should use Column
for alignment purposes. A better way is to use Align widget as shown below:
Container(
height: 30,
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(15),
),
child: Align(
alignment: Alignment.center,
child: Text(
'hallo',
style: TextStyle(background: _paint),
),
),
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With