11

Hi Flutter community:)

Working on a flutter app and seeking help with UI widget.

I'm lost on how to set child's height according to a parent's height.

Need to create a vertical divider(or Container with custom height) and set it's height to maximum of its parent because the parent height(which is a column in my case) will change depending on inside widgets.

I've found ways of creating vertical divider but with a fixed height. Tried using BoxFit, BoxConstraints, FittedBox and few other ways and failed to set height of a parent.

The divider is placed inside a Container>Row>Column->Container and divider's height should be the height of a Column.

As in example of this image:

https://i.stack.imgur.com/uUWjF.png

p.s. all widget is placed inside a ListView

      Column(
         children: <Widget>[
            Container(
              color: Colors.blue,
              width: 5.0,
              //height: -> setting to maximum of its parent
            ),
          ],
        ),
petras J
  • 2,538
  • 3
  • 16
  • 23

4 Answers4

15

You can use IntrinsicHeight for this.

I used Container for my divider in this example, because I needed border radius on my divider, but you can use VerticalDivider too.

IntrinsicHeight(
        child: Row(
          children: <Widget>[
            // This is your divider
            Container(
              width: 3,
              height: double.infinity,
              color: Colors.blue,
              margin: const EdgeInsets.only(right: 4),
            ),
            Column(
              children: <Widget>[
                Text(
                  'Text 1',
                  style: TextStyle(fontSize: 20),
                ),
                Text(
                  'Text 2',
                  style: TextStyle(fontSize: 20),
                ),
                Text(
                  'Text 3',
                  style: TextStyle(fontSize: 20),
                ),
              ],
            ),
          ],
        ),
      ),

This is the result

If you just need a simple divider on the side I would suggest that you add border to the Container, because documetnation for IntrinsicHeight says is expensive to call, so avoid using it where possible.

Here you can read more about adding border to the Container.

EDIT: 25.08.2022.

Flutter's official channel video explaining Intrinsic widgets

merfire
  • 226
  • 2
  • 9
0

You can use expanded widget to use full height of the parent widget.

Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61
0

You should fist determine how many items in yout listtile after that you can use this

you can use this container for draw a line

for (var i in yourItem) Dividers(i),

Container Dividers(Map<String, dynamic> changes) {
  return Container(
    decoration: BoxDecoration(
      border: Border(
        top: BorderSide(width: 30, color: Colors.blue),
        bottom: BorderSide(width: 30, color: Colors.blue),
      ),
      color: Colors.white,
    ),
    margin: EdgeInsets.only(left: 22),
    width: 2,
  );
}
Anand
  • 4,355
  • 2
  • 35
  • 45
-5

You can use a Container and do not specify the height neither the constraints the container will expand to fill the space in its parent.

Container(
    child: Divider(
        width: 5.0, 
        color: Colors.blue,
    )
)
Arlind Hajredinaj
  • 8,380
  • 3
  • 30
  • 45
  • It's a possibility but divider only has height parameter, but by rotating to vertical it might work. – petras J Aug 31 '18 at 11:29