39

To occupy the whole width or height , I use double.infinity, but going through some of the flutter samples, I have noticed many people use MediaQuery.of(context).size.width or height. I went through the docs but there are no differences mentioned between this two or when to use which one as both allow same feature. Are they specific to particular widgets?

  • @pskink whichever widgets have width and height property –  Feb 02 '19 at 07:30
  • @pskink but i canuse MediaQuery as well with Containers –  Feb 02 '19 at 08:12
  • @pskink yeah i get that, but whats the difference between two, when to use what and are there any performance caveats related to them –  Feb 02 '19 at 09:21
  • @pskink please read the question heading again –  Feb 02 '19 at 09:42

2 Answers2

96

The difference can be summarized into:

  • I want to be as big as my parent allows (double.infinity)
  • I want to be as big as the screen (MediaQuery).

Usually, you'll want to use double.infinity, but it's not always possible.

Some Widgets allow their children to be as big as they want to be (Column, ListView, OverflowBox...). In that situation using double.infinity creates a paradox:

  • The parent allows any size
  • The child wants the biggest size allowed by the parent

Using MediaQuery in these situations is bad though. You will rarely want to do that unless you're creating a widget similar to Scaffold.

That's where widgets such as IntrinsincHeight comes in handy. These widgets are able to solve the paradox, and therefore have a valid layout.

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
2

MediaQuery occupies the space as per the screen size whereas double.infinity occupies the space as parent size. It could be matched with match-parent in Android.

Mustafa
  • 27
  • 3
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 27 '22 at 10:20