3

In this question Whats the difference between double.infinity and MediaQuery? Rémi Rousselet suggested to use IntrinsicHeight Widget instead of MediaQuery. I want to understand the advantage of IntrinsicHeight over MediaQuery as when I read the documentation I found this-

IntrinsicHeight: This class is relatively expensive, because it adds a speculative layout pass before the final layout phase. Avoid using it where possible. In the worst case, this widget can result in a layout that is O(N²) in the depth of the tree.

So the above says that the later is more expensive and should be avoided wherever possible, then why should we use IntrinsicHeight instead of simply MediaQuery?

Intkhab Ahmed
  • 118
  • 1
  • 8

2 Answers2

5

IntrinsicHeight and MediaQuery have two completely different purpose.

  • MediaQuery gives information about the screen. It is then up to you to use this information to change your UI (although I would recommend using double.infinity or LayoutBuilder).

  • IntrinsicHeight is a layout utility. It is used for when you want a widget to have its "ideal minimum" size instead of the actual minimum size.

    A common use-case for IntrinsicHeight is for when you want all items inside a Row to have the same "height" when that "height" is the size of the biggest item.

    If the size of your items, then using IntrinsicHeight is the only solution to achieve such a thing.

    This would typically look like:

IntrinsicHeight(
  child: Row(
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: [ Whatever(), AnotherItem() ], 
  ),
)
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
2

IntrinsicHeight => TAkes height as the most largest of the heigts of the children widgets (Example Rows Childrens) It might not be total height of screen. Needs to be calculate for each widget. REad details https://medium.com/flutter-community/flutter-layout-cheat-sheet-5363348d037e

MediaQuery is calculated on screen size not for widgets. REad details https://medium.com/flutter-community/a-guide-to-using-screensize-in-flutter-a-more-readable-approach-901e82556195

Dev
  • 6,628
  • 2
  • 25
  • 34