title: Flutter save widget directly into an image
body: <p>I have a custom widget that I want to save into a png image.
  The issue I'm facing is that with the current implementation is that I'm required to show the widget on the screen.
  What I want is to save the widget directly into the image without showing it.</p>
  <p>As a workaround I'm saving the image at the first possible moment when it renders on the screen then quickly dismiss it.</p>
  <p>This is how I save the widget now :</p>
  <pre><code>
  class SomeWidget extends StatefulWidget {
    const SomeWidget({
      Key key,
    }) : super(key: key);
  
    @override
    _ShareCocktailMockState createState() =&gt; _ShareCocktailMockState();
  }
  
  class _SomeWidgetState extends State&lt;SomeWidget&gt;
      with AfterLayoutMixin&lt;SomeWidget&gt; {
    GlobalKey globalKey = GlobalKey();
  
    Future&lt;void&gt; _capturePng() async {
      RenderRepaintBoundary boundary =
          globalKey.currentContext.findRenderObject();
      try {
        if (boundary.debugNeedsPaint) {
          print(&quot;Waiting for boundary to be painted.&quot;);
          await Future.delayed(const Duration(milliseconds: 5));
          return _capturePng();
        }
      } catch (_) {}
      try {
        ui.Image image = await boundary.toImage();
        ByteData byteData =
            await image.toByteData(format: ui.ImageByteFormat.png);
        Uint8List pngBytes = byteData.buffer.asUint8List();
  
        // SHARING IMAGE TO SOCIAL MEDIA
        // CODE
  
        // widget is presented with a dialog so I just pop it
        Navigator.of(context).pop();
      } catch (_) {
        await Future.delayed(const Duration(milliseconds: 5));
        return _capturePng();
      }
    }
  
    @override
    void afterFirstLayout(BuildContext context) {
      _capturePng();
    }
  
    @override
    Widget build(BuildContext context) {
      return RepaintBoundary(
        key: globalKey,
        child: SuperFancyWidget(),
      );
    }
  }
  </code></pre>
  <p>afterFirstLayout is from <a href="https://pub.dev/packages/after_layout" rel="noreferrer">https://pub.dev/packages/after_layout</a> package</p>