MeLean
MeLean

Reputation: 3451

How to underline a Container Widget in Flutter

I am trying to underline a Container in my Flutter app. So far I achieved a some kind of underling when I used the following code:

    Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: Padding(
                padding: EdgeInsets.all(8.0),
                child: Text(
                  'Underline my parent!',
                  maxLines: 2,
                  textAlign: TextAlign.center,
                ),
              ),
            )
          ],
        ),
        decoration: Border(bottom: BorderSide(color: Colors.grey)),
      ),

But now I want the underline dash not being from start to end, I want to have space on the start and on the end. If there is some smarter way to underline widgets I would be glad to see it too.

Upvotes: 9

Views: 18983

Answers (5)

Germain Kataku
Germain Kataku

Reputation: 29

User this const Divider(color: Colors.black54),

Upvotes: 0

GraSim
GraSim

Reputation: 4230

Add a bottom BorderSide to your container.

     Container(
        decoration: BoxDecoration(
           border: Border(
              bottom: BorderSide(width: 1.0, color: Colors.black),
           ),
       ),
    ),

Upvotes: 27

Miko
Miko

Reputation: 862

You can simply to create underline in Container widget using Border

here the code:

Container(
  padding: EdgeInsets.all(8.0),
  decoration: BoxDecoration(
    border: Border(
      bottom: BorderSide(
        width: 1.0
      ),
    ),
  ),
  child: Text(
    'Underline my parent!',
    maxLines: 2,
    textAlign: TextAlign.center,
  ),
), 

Upvotes: 5

Bostrot
Bostrot

Reputation: 6033

You can use a simple Divider widget including a padding:

new Padding(
    padding: EdgeInsets.all(8.0), 
    child: new Divider()
),

You can then wrap your existing widget with a column:

new Column(
    children: <Widget> [
        yourContainerWidget,
        new Padding(
            padding: EdgeInsets.all(8.0), 
            child: new Divider()
        ),     
    ]
)

Upvotes: 7

Blasanka
Blasanka

Reputation: 22477

One simple solution is to create a line using Container widget. And wrap Row widget using a Column widget and add that line as the second child, like below:

var aLine = Container(color: Colors.grey, width: 50.0, height: 2.0,); 

Column(
  children: <Widget>[
    Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Expanded(
            child: Padding(
              padding: EdgeInsets.all(8.0),
              child: Text(
                'Underline my parent!',
                maxLines: 2,
                textAlign: TextAlign.center,
              ),
            ),
          ),
        ],
      ),
      aLine
  ],
),

Upvotes: 1

Related Questions