4

RenderBox gives the opportunity to overide paint() method. But how to build() Widget from that RenderBox?

1 Answer 1

4

Instead of StatefulWidget or StatelessWidget you can subclass SingleChildRenderObjectWidget (or similar)

class MyWidget extends SingleChildRenderObjectWidget {
  @override
  MyRenderBox createRenderObject(BuildContext context) {
    return new MyRenderBox(title: "bar");
  }

  @override
  void updateRenderObject(BuildContext context, MyRenderBox renderObject) {
    renderObject.title = "foo";
  }
}

class MyRenderBox extends RenderBox {
  String title;

  MyRenderBox({this.title});
}

A few other interesting subclasses are LeafRenderObjectWidget and MultiChildRenderObjectWidget

Sign up to request clarification or add additional context in comments.

3 Comments

Still doesn't work... I/flutter ( 601): This RenderObject has no descendants. I/flutter ( 601): ══════════ I/flutter ( 601): Another exception was thrown: RenderBox was not laid out: MyRenderBox#ec2de relayoutBoundary=up3 NEEDS-PAINT
It did work. But then you need to implement something inside MyRenderBox. That's out of the scope of this question though
I tried SingleChildRenderObject and LeafRenderObject.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.