Why can LayoutBuilder not have an intrinsic dimensions?

Viewed 3065

In the docs of _RenderLayoutBuilder in layout_builder.dart it says:

'LayoutBuilder does not support returning intrinsic dimensions.\n'
'Calculating the intrinsic dimensions would require running the layout '
'callback speculatively, which might mutate the live render object tree.'

What is the “layout callback” in this case, and why would it have to be called speculatively?

I am trying to wrap my head around how to implement a custom RenderBox that similarly to LayoutBuilder builds its child using a builder callback and supports intrinsic dimensions.

2 Answers

The "layout callback" is the builder function you pass to the LayoutBuilder.

Calculating the intrinsic dimensions of any RenderObject involves a speculative layout pass. LayoutBuilder builds its widget subtree at layout time, which can mutate the render object tree if its contraints have changed. Since this speculative layout pass almost certainly uses different constraints than what will ultimately be used in the layout, it is impossible for LayoutBuilder to work the way you want.

This is fundamental to how LayoutBuilder works, so I doubt you'd be able to modify it to support intrinsic dimensions. Depending on what layout changes you plan to support, you might consider basing this custom RenderBox on CustomMultiChildLayout instead.

You can try to wrap widget with Column and set mainAxisSize: MainAxisSize.min, so that high arlertDialog changes according to its intrinsic.

enter image description here

Related