獲取組件高度 AppBar 高度
方法一 以 AppBar 為例
Widget build(BuildContext context) {
// 將 AppBar 定義為變量
var appBar = AppBar(
title: const Text('XXX'),
);
// 獲取高度
print(appBar.preferredSize.height);
return Scaffold(appBar: appBar,
body: Container());
}
方法二 使用 GlobalKey
final GlobalKey globalKey = GlobalKey();
Stack(
key: globalKey,
....
)
globalKey.currentContext?.size
獲取狀態(tài)欄高度
MediaQuery.of(context).padding.top