我正在尝试为我在应用程序中实现的小部件设置动态大小,我目前正在使用:
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
只需简单地执行此操作即可单独获取脚手架主体的大小(在本例中为高度(。
final fullHeight = MediaQuery.of(context).size.height;
final appBar = AppBar(); //Need to instantiate this here to get its size
final appBarHeight = appBar.preferredSize.height + MediaQuery.of(context).padding.top;
final scaffoldBodyHeight = fullHeight - appBarHeight;
注意:appBarHeight是应用栏的高度和设备状态栏的高度之加。
脚手架身体高度是你的脚手架身体的高度!