How to get widget\'s absolute coordinates on a screen in Flutter?
Or its offset in a parent
Example:
import \'package:flutter/material.dart\'
Maybe this simple widget is what you want : rect_getter 0.0.1
This is a widget provide a simple way to get child's rectangle information after rendered.
You can use this extension I wrote (requires Dart 2.6):
extension GlobalKeyExtension on GlobalKey {
Rect get globalPaintBounds {
final renderObject = currentContext?.findRenderObject();
var translation = renderObject?.getTransformTo(null)?.getTranslation();
if (translation != null && renderObject.paintBounds != null) {
return renderObject.paintBounds
.shift(Offset(translation.x, translation.y));
} else {
return null;
}
}
}
final containerKey = GlobalKey();
Container(
key: containerKey,
width: 100,
height: 50,
)
void printWidgetPosition() {
print('absolute coordinates on screen: ${containerKey.globalPaintBounds}');
}