DEV Community

kyorohiro (kiyohiro kawamura)
kyorohiro (kiyohiro kawamura)

Posted on • Edited on

4

[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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay