Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically align Text inside Container

Tags:

flutter

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:

like image 758
marcin Avatar asked Jan 13 '19 21:01

marcin


People also ask

How do I vertically align text in a container?

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.

How do I align text in the middle of a container?

To just center the text inside an element, use text-align: center; This text is centered.

How do I vertically align text in a div?

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 .


1 Answers

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),
    ),
  ),
),
like image 111
Abu Saad Papa Avatar answered Sep 19 '22 11:09

Abu Saad Papa