MediaQuery.of(context).padding.top is returning 0.0 when I'm using it with appBar. Why is that?

Viewed 2659

When I use MediaQuery.of(context).padding.top without the navBar in the widget tree then it returns me the actual true value.

Reloaded 1 of 529 libraries in 637ms.
I/flutter (31730): 24.0

But when I place the appBar into the widget tree then it returns me 0.0 only.

Reloaded 1 of 529 libraries in 764ms.
I/flutter (31730): 0.0

Can someone please help that why it is happening?

1 Answers

From this property's docs:

If you consumed this padding (e.g. by building a widget that envelops or accounts for this padding in its layout in such a way that children are no longer exposed to this padding), you should remove this padding for subsequent descendants in the widget tree by inserting a new MediaQuery widget using the MediaQuery.removePadding factory.

As the top padding is accounted for by the navBar, it is no longer exposed to widgets below it.

Related