oravecz
oravecz

Reputation: 1206

A responsive MultiChildRenderObjectWidget in Flutter

I have a MultiChildRenderObjectWidget which will position some children above or below the bottom of the screen height dimension. I pass the children into my widget and initially layout the children based on the screen dimensions I can access because I am passed a BuildContext in the RenderObject createRenderObject(BuildContext context) function. It seems to work well.

However, when the screen size changes, how can I re-execute the performLayout on my RenderObject in order for it to relayout the children for the new screen size?

class AuthContent extends MultiChildRenderObjectWidget {
  AuthContent({
    Key key,
    this.background,
    this.actionBar,
    this.loginForm,
    this.disclosure,
  })  : assert(background != null),
        assert(actionBar != null),
        assert(loginForm != null),
        assert(disclosure != null),
        super(
            key: key, children: [background, actionBar, disclosure, loginForm]);

  final Background background;
  final ActionBar actionBar;
  final Disclosure disclosure;
  final LoginForm loginForm;

  @override
  RenderObject createRenderObject(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return RenderAuthContent(screenSize: screenSize);
  }
}

Upvotes: 2

Views: 1615

Answers (1)

oravecz
oravecz

Reputation: 1206

Found a good example of working with RenderObject in Nick's How to Create a Flutter Widget Using a RenderObject article.

The thing I was missing was the updateRenderObject method. By overriding this, I am able to pass new properties to the RenderObject which it can use during the performLayout method.

  @override
  void updateRenderObject(
      BuildContext context, RenderAuthContent renderObject) {
    renderObject..screenSize = screenSize;
  }

In my case, I care about the change in screen size.

Upvotes: 3

Related Questions