【问题标题】:MediaQuery.of(context).padding.top is returning 0.0 when I'm using it with appBar. Why is that?当我将 MediaQuery.of(context).padding.top 与 appBar 一起使用时,它返回 0.0。这是为什么?
【发布时间】:2021-04-08 03:54:47
【问题描述】:

当我在小部件树中使用没有导航栏的MediaQuery.of(context).padding.top 时,它会返回实际的真实值。

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

但是当我将 appBar 放入小部件树时,它只返回 0.0。

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

有人可以帮忙解释一下为什么会这样吗?

【问题讨论】:

    标签: android ios flutter dart emulation


    【解决方案1】:

    来自该属性的docs

    如果您使用了此填充(例如,通过构建一个小部件,在其布局中封装或说明此填充,使子级不再暴露于此填充),您应该为小部件中的后续后代删除此填充通过使用 MediaQuery.removePadding 工厂插入新的 MediaQuery 小部件来创建树。

    由于顶部填充由导航栏占据,它不再暴露给它下面的小部件。

    【讨论】:

    • 谢谢,我现在明白了。在将 navBar 添加到小部件树之前,我可以通过计算它来收集状态栏大小。 :)
    • 你能提供一个代码sn-p吗?
    猜你喜欢
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 2014-04-07
    • 2020-06-27
    相关资源
    最近更新 更多