给globalkey增加一个扩展
extension GlobalKeyExtension on GlobalKey {
Rect? get globalPaintBounds {
final renderObject = currentContext?.findRenderObject();
final matrix = renderObject?.getTransformTo(null);
if (matrix != null && renderObject?.paintBounds != null) {
final rect = MatrixUtils.transformRect(matrix, renderObject!.paintBounds);
return rect;
} else {
return null;
} } }
使用方法
final containerKey = GlobalKey();
Container(
key: containerKey,
width: 100,
height: 50,
)
void printWidgetPosition()
{
print('absolute coordinates on screen: ${containerKey.globalPaintBounds}');
}
网友评论