[英]MediaQuery.of(context).padding.top is returning 0.0 when I'm using it with appBar. Why is that?
When I use MediaQuery.of(context).padding.top
without the navBar in the widget tree then it returns me the actual true value.当我在小部件树中使用没有 navBar 的
MediaQuery.of(context).padding.top
时,它会返回实际的真实值。
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.但是当我将 appBar 放入小部件树时,它只返回 0.0。
Reloaded 1 of 529 libraries in 764ms.
I/flutter (31730): 0.0
Can someone please help that why it is happening?有人可以帮忙解释为什么会这样吗?
From this property's docs :从这个属性的文档:
If you consumed this padding (eg 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.
如果您使用了此填充(例如,通过构建一个在其布局中封装或说明此填充的小部件,以使子级不再暴露于此填充),您应该通过插入为小部件树中的后续后代删除此填充使用 MediaQuery.removePadding 工厂的新 MediaQuery 小部件。
As the top padding is accounted for by the navBar, it is no longer exposed to widgets below it.由于顶部填充由导航栏占据,它不再暴露给它下面的小部件。
In my case, I passed the padding
value.就我而言,我传递了
padding
值。 and I consumed the padding.top
in my Page1.我在 Page1 中使用了
padding.top
。
Widget build(BuildContext context) {
final padding = MediaQuery.of(context).padding;
return SafeArea(
child: Scaffold(
body: IndexedStack(
children: [
Page1(padding),
Page2(),
Page3(),
Page4(),
],
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.