flutter 中获取组件高度
flutter 获取组件宽高
·
AppBar的高度获取
Widget build(BuildContext context) {
// 将 AppBar 定义为变量
var appBar = AppBar(
title: const Text('title'),
);
// 获取高度
print(appBar.preferredSize.height);
return Scaffold(appBar: appBar);
}
使用GlobalKey()
final GlobalKey globalKey = GlobalKey();
Stack(
key: globalKey,
....
)
// 获取高度
globalKey.currentContext?.size
// 获取偏移量
RenderBox box = globalKey
.currentContext
?.findRenderObject() as RenderBox;
Offset offset = box.localToGlobal(Offset.zero);
double height = offset.dy;
状态栏高度
MediaQuery.of(context).padding.top
底部栏高度
有点手机获取为0,如iPhone6s
double bottomPadding = MediaQuery.of(context).padding.bottom;
屏幕宽高
Size screenSize = MediaQuery.of(context).size;
更多推荐
已为社区贡献2条内容
所有评论(0)