也可参考 https://www.jianshu.com/p/8117fbc5b4d3
1.获取状态栏高度:
MediaQueryData.fromWindow(WidgetsBinding.instance.window).padding.top
2.获取AppBar的高度-kToolbarHeight
import 'package:flutter/src/material/constants.dart';
/// * [kMinInteractiveDimensionCupertino]
/// * The Material spec on touch targets at <https://material.io/design/usability/accessibility.html#layout-typography>.
const double kMinInteractiveDimension = 48.0;
/// The height of the toolbar component of the [AppBar].
const double kToolbarHeight = 56.0;
/// The height of the bottom navigation bar.
const double kBottomNavigationBarHeight = 56.0;
3.获取widget所在的位置,即偏移的xy值;
这个偏移值附加说一句,如果有appBar,这个值会包含(状态栏+appBar的高度)
getX(GlobalKey key) {
RenderBox renderBox = key.currentContext.findRenderObject();
double dx = renderBox.localToGlobal(Offset.zero).dx;
return dx;
}
getY(GlobalKey key) {
RenderBox renderBox = key.currentContext.findRenderObject();
double dy = renderBox.localToGlobal(Offset.zero).dy;
return dy;
}
4.获取widget的宽高
getWidth(GlobalKey key) {
RenderBox renderBox = key.currentContext.findRenderObject();
return renderBox.size.width;
}
getHeight(GlobalKey key) {
RenderBox renderBox = key.currentContext.findRenderObject();
return renderBox.size.height;
}