Why does ListView ignore constraints

Viewed 453

Why does a ListView completely ignore imposed constraints?

  @override
  Widget build(BuildContext context) {
    return Padding(
      child: Container(
        constraints: BoxConstraints(maxWidth: 500),
        child: ListView(
          children: buildList(data),
        ),
      ),
    );
  }

The displayed ListView does not seem to be bothered at all with the maxWidth:500 of the parent container (same if replaced with a ConstrainedBox) and all elements (which are Rows) go full screen width.

Why?

The solution is to work with width of children instead, but still I'd much rather constrain the entire listView, and also if there's a constraint, shouldn't everything inside be forced not to exceed it?

EDIT: After hours of trying various things it finally works. Turns out the list view needed to a child of a Center, otherwise it would not pay any attention to SizedBoxes, ConstrainedBoxes, Containers etc. you'd put on it or its children and stretch full screen no matter what.

1 Answers

What are the parent widgets of the Padding(child: Container(... list view ))? I mean are you sure that a parent is not imposing its constraints on its children? Try do replace your ListView with Container(color: Colors.red, height: 100) and see if it respects the constraints. If it does not, there probably is a parent widget with infinite width constraint or something like that!

A solution can be wrap your Container with an UnconstrainedBox but I would recommend first checking my first point :D

Related