get position of widget flutter code example

Example 1: get position of a widget in screen flutter

extension GlobalKeyEx 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;
    }
  }
}

Example 2: get position of a widget in screen flutter

@override  void initState() {    WidgetsBinding.instance.addPostFrameCallback(_afterLayout);    super.initState();  } _afterLayout(_) {    _getSizes();    _getPositions();  }

Example 3: get position of a widget in screen flutter

//creating Key for red panelGlobalKey _keyRed = GlobalKey();...//set key    Flexible(             flex: 2,             child: Container(               key: _keyRed,               color: Colors.red,             ),     ),

Tags:

Misc Example