简体   繁体   English

当我将 MediaQuery.of(context).padding.top 与 appBar 一起使用时,它返回 0.0。 这是为什么?

[英]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.

相关问题 未检测到键盘。 MediaQuery.of(context).viewInsets.bottom 总是返回 0.0 - Keyboard not being detected. MediaQuery.of(context).viewInsets.bottom always returns 0.0 MediaQuery.of(context) 在 Flutter 应用程序中导致错误 - MediaQuery.of(context) causing error in Flutter app 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 错误 - MediaQuery.of() called with a context that does not contain a MediaQuery. error Flutter Get.width 或初始化 MediaQuery.of(context).size 在 Release 模式下不起作用 - Flutter Get.width or initialize MediaQuery.of(context).size not working in Release mode Flutter MediaQuery.of(context).size.width 检查设备大小总是在 iOS 模拟器中返回相同的错误值 - Flutter MediaQuery.of(context).size.width to check device-size always returns same wrong value in the iOS Simulator 当我使用其他应用程序时,Android活动显示在顶部 - Android Activity appears on top when I'm using another app 为什么我在使用 JetPack Compose 时不能使用 Modifier.padding(top = 20.dp) 来增加 AlertDialog 中的空间? - Why can't I use Modifier.padding(top = 20.dp) to increase space in AlertDialog when I use JetPack Compose? Flutter - AppBar 上的填充 - Flutter - Padding on AppBar 为什么在使用add时以前的片段没有动画? - Why previous fragment does not animate when I'm using add ? 为什么我得到 Lat Long 0.0? - Why I am getting Lat Long 0.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM