DEV Community

kyorohiro (kiyohiro kawamura)
kyorohiro (kiyohiro kawamura)

Posted on • Updated on

[Memo] Dart x Flutter: Get widget offset and size in real time

When you change the screen size with Flutter for Web
When you want to overwrite arbitrary DOM on Widget

class MyContainer extends SingleChildRenderObjectWidget {
  MyContainer({Key key,Widget child}):super(key:key, child:child);

  double lastDx = 0.0;
  double lastDy = 0.0;
  double lastDw = 0.0;
  double lastDh = 0.0;

  @override
  RenderObject createRenderObject(BuildContext context) {
    return MyContainerRenderObject(this);
  }
}

class MyContainerRenderObject extends RenderProxyBox {
  MyContainer myparent;

  MyContainerRenderObject(this.myparent) {

  }

  @override
  void paint(PaintingContext context, Offset offset) {
    assert(!debugNeedsLayout);
    this.myparent.lastDx = offset.dx;
    this.myparent.lastDy = offset.dy;
    this.myparent.lastDw = size.width;
    this.myparent.lastDh = size.height;
    print("size: ${this.size}");
    print("offset ${offset.dx} ${offset.dy}");
    super.paint(context, offset);
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)