Flutter 在页面启动完成获取widget组件大小位置(非手
2020-06-09 本文已影响0人
梁典典
flutter自学交流群1102781545,欢迎提问
只需要在页面顶层套一个组件
onNotification: (notification) {
if (_topAppbarHei == 0) {
setState(() {
_topAppbarHei = _appbarGlogbalKey.currentContext.size.height +
MediaQueryData.fromWindow(window).padding.top;
_initImagesTopHei = getY(_detailImagesGlogbalKey.currentContext); // 获取widget到屏幕顶部的距离
});
addScrollListener(); // 滑动监听
}
return null;
},
child:... // 子组件
获取widget到屏幕顶部距离方法
double getY(BuildContext buildContext) {
final RenderBox box = buildContext.findRenderObject();
final topLeftPosition = box.localToGlobal(Offset.zero);
return topLeftPosition.dy;
}