2022年9月21日星期三

get object position on screen

extension GlobalKeyExtension on GlobalKey {
  Rect? get globalPaintBounds {
    final renderObject = currentContext?.findRenderObject();
    final translation = renderObject?.getTransformTo(null).getTranslation();
    if (translation != null && renderObject?.paintBounds != null) {
      final offset = Offset(translation.x, translation.y);
      return renderObject!.paintBounds.shift(offset);
    } else {
      return null;
    }
  }
}




final containerKey = GlobalKey();

Container(
  key: containerKey,
  width: 100,
  height: 50,
)

containerKey.globalPaintBounds

沒有留言:

發佈留言