9

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.

MeLean
  • 3,092
  • 6
  • 29
  • 43

5 Answers5

26

Add a bottom BorderSide to your container.

     Container(
        decoration: BoxDecoration(
           border: Border(
              bottom: BorderSide(width: 1.0, color: Colors.black),
           ),
       ),
    ),
GraSim
  • 3,830
  • 1
  • 29
  • 35
7

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()
        ),     
    ]
)
Bostrot
  • 5,767
  • 3
  • 37
  • 47
5

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,
  ),
), 
Miko
  • 643
  • 8
  • 13
1

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
  ],
),
Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

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