如何在 Flutter 中获取 AppBar 高度

新手上路,请多包涵

如何在 Flutter 中获取 AppBar 的高度?

我正在使用 MarialApp 小部件(’package:flutter/material.dart’)。

我有我的 Context 的高度,想减去 appbar 的高度。

 final double height = MediaQuery.of(context).size.height;

原文由 R2T8 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2k
2 个回答

我认为这不是一个理想的方法,但它会起作用。

首先 声明 AppBar 您将在 Scaffold 中使用的小部件。

 Widget demoPage() {
  AppBar appBar = AppBar(
    title: Text('Demo'),
  );
  return Scaffold(
    appBar: appBar,
    body: /*
    page body
    */,
  );
}

现在 您可以使用 preferredSized 获得 appBar高度

 double height = appBar.preferredSize.height;

您可以使用此高度来减少 屏幕高度

 final double height = MediaQuery.of(context).size.height;

原文由 ap14 发布,翻译遵循 CC BY-SA 4.0 许可协议

在 Widget 构建中,您需要获取标准 AppBar 的高度和宽度,然后使用这些值声明一个 Size 对象,然后输入高度和宽度作为参数。

您可以对我称为 MyAppBar() 的自定义 AppBar 执行以下操作

Widget build(BuildContext context) {
//get the height and width of a standard appBar to use with custom Appbar

var appBarWidth = MediaQuery.of(context).size.width;
var appBarHeight = AppBar().preferredSize.height;

return Scaffold(
  appBar: PreferredSize(
    preferredSize: Size(appBarWidth, appBarHeight),
    child: MyAppBar(),
  ),

原文由 Jason Braithwaite 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题