有可能得到脚手架的body高度和宽度吗?



我正在尝试为我在应用程序中实现的小部件设置动态大小,我目前正在使用:

MediaQuery.of(context).size.width/height

这为您提供了屏幕的大小,但我需要小部件基于脚手架主体的大小而不是全屏

您可以使用脚

手架的构建器:

return Scaffold(
  appBar: AppBar(),
  body: Builder(
    builder: (context) {
      return Stack(
        children: <Widget>[

然后:

bodyHeight = MediaQuery.of(context).size.height - Scaffold.of(context).appBarMaxHeight

至少,我以这种方式找到了解决方案。

是的,您可以使用 layoutBuilder 小部件来完成。检查这个:

https://medium.com/@KarthikPonnam/flutter-layoutbuilder-widget-1-b09fd1e6907f

这是正确获取脚手架身高的方法

首先,获取应用栏高度。您需要为它使用变量。

var appBar = AppBar(
      title: Text('Testing'),
    );

现在,按照以下代码 [基本上=> 总高度 - 应用栏的高度 - 顶部的填充(应用状态栏的高度(]

final bodyHeight = MediaQuery.of(context).size.height -
                  -appBar.preferredSize.height -
                  MediaQuery.of(context).padding.top

如何使用?假设您有 2 个孩子的列

Column(children: [
        Container(
          height: bodyHeight * 0.7,
          child: ...,
        ),
        Container(
          height: bodyHeight * 0.3,
          child: ...,
        ),
      ],
)
您还可以

从 MediaQuery.of(context(.size.height

最新更新