Flutter paint(): is this legit? Rendering Renderobject in different context

1.5k views Asked by At

I try to render the child of a listitem (somewhere up) to a different place (widget) in the tree; In the approach below, BlendMask is the "target" widget that checks for and paints "source" widgets that got themself an GlobalKey which is stored in blendKeys. This works somewhat. And I'm not quite sure if I might fight the framework, or just missing some points...

The problems are two:

  • The minor one: This approach doesn't play nice with the debugger. It compiles and runs fine but every hot-reload (on save f.e.) throws an "can't findRenderObject() of inactive element". Maybe I miss some debug flag?

  • the real problem, that brought me here questioning the idea en gros: As mentioned, the Source-Widget is somewhere in the subtree of the child of a Scrollable (from a ListView.build f.e.): How can I update the Òffset for the srcChild.paint() when the list is scrolled? - without accessing the lists scrolController?! I tried listening via WidgetsBindingObservers didChangeMetrics on the state of the Source widget, but as feared no update on scroll. Maybe a strategically set RepaintBounderyis all it needs? *hope* :D

Anyway, every tip much appreciated. Btw the is an extend of this question which itself extends this...

class BlendMask extends SingleChildRenderObjectWidget {
  [...]
  @override
  RenderObject createRenderObject(context) {
    return RenderBlendMask();
  }
}

class RenderBlendMask extends RenderProxyBox {
[...]
@override
void paint(PaintingContext context, offset) {    <-- the target where we want to render a widget
  [...]                                              from somewhere else in the tree!
  for (GlobalKey key in blendKeys) {
    if (key.currentContext != null) {
      RenderObject? srcChild                     <-- the source we want to render in this sibling widget!
       = key.currentContext!.findRenderObject();
      if (srcChild != null) {
        Matrix4 mOffset = srcChild.getTransformTo(null);
        context.pushTransform(true, offset, mOffset, (context, offset) {
          srcChild.paint(context, offset);
        });
      }
    } 
  }
}
} //RenderBlendMask
0

There are 0 answers