如何在 Flutter 中获取 AppBar 的高度?
我正在使用 MarialApp 小部件(’package:flutter/material.dart’)。
我有我的 Context 的高度,想减去 appbar 的高度。
final double height = MediaQuery.of(context).size.height;
原文由 R2T8 发布,翻译遵循 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 许可协议
2 回答2k 阅读✓ 已解决
2 回答1.3k 阅读✓ 已解决
2 回答1k 阅读
2 回答2.6k 阅读
1 回答1.1k 阅读✓ 已解决
1 回答2.7k 阅读
2 回答762 阅读✓ 已解决
我认为这不是一个理想的方法,但它会起作用。
首先 声明
AppBar
您将在Scaffold
中使用的小部件。现在 您可以使用
preferredSized
获得appBar
的 高度:您可以使用此高度来减少 屏幕高度。