4

Summary

When I have my custom widget inside a row that is inside an Expanded widget (Expanded > Row > CustomWidget), it works great. But if I then wrap the custom widget inside a column that is inside the row (Expanded > Row > Column > CustomWidget), it throws an assertion: 'BoxConstraints forces an infinite width and infinite height.'

I understand the gist of the assertion, but I don't understand why the addition of the column between the row and custom widget caused the problem when it wasn't there before. Without the column, the constraints must be getting passed from parent to child down the tree. Putting the column in there seems to break that chain.

Attempted Solutions

I have tried:

  • setting the column mainAxisSize: MainAxisSize.min

  • wrapping the column in Expanded (between Row and Column widgets)

  • using a LayoutBuilder to create a SizedBox with height of BoxConstraints' maxHeight that wraps the Column

  • using a ConstrainedBox (also with LayoutBuilder)

Code Snippet

Expanded(
  flex: 2,
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    crossAxisAlignment: CrossAxisAlignment.center,
    mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text('Title goes here'),
          Stack(
            alignment: Alignment.center,
            children: <Widget>[
              restWidget,
              if (!_appBarStopwatch.isRunning)
                ...startButton,
            ],
          ),
        ],
      ),
    ],
  ),
),

restWidget is a variable referencing a custom widget of type RestIndicator

Expanded is a child of the following tree SafeArea > Padding > Column.

Error

flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during performLayout():
flutter: BoxConstraints forces an infinite width and infinite height.
.
.
.
flutter: The following RenderObject was being processed when the exception was fired:
flutter:   RenderConstrainedBox#7d17f relayoutBoundary=up10 NEEDS-LAYOUT NEEDS-PAINT
flutter:   creator: SizedBox.expand ← Stack ← LayoutBuilder ← WorkoutRestIndicator ← StreamBuilder<Duration>
flutter:   ← StreamBuilder<RestStep> ← RestIndicator ← Stack ← Column ← Row ← Expanded ← Column ← ⋯
flutter:   parentData: not positioned; offset=Offset(0.0, 0.0) (can use size)
flutter:   constraints: BoxConstraints(unconstrained)
flutter:   size: MISSING
flutter:   additionalConstraints: BoxConstraints(biggest)

Removing the Column widget from the tree solves the error but then I can't create the design I want. Is there a way to re-establish the constraints so it works as it did before I introduced the Column? Do I need to abandon the use of Expanded?

Tres
  • 107
  • 1
  • 9

0 Answers0