6

I have a widget with an image and some information that I would like the user to be able to save and share as an image.

Does anyone know how to accomplish this without specifically telling the user to physically take the screenshot?

Chamanhm
  • 1,060
  • 1
  • 11
  • 17
  • It rather depends on how you got the image into the Widget. If it came from an asset, or you are able to download it, you can use the solution here https://stackoverflow.com/questions/44181343/how-do-i-share-an-image-on-ios-and-android-using-flutter If you drew the image yourself using CustomPaint there doesn't seem to be a way to extract the necessary bitmap. – Richard Heap Apr 26 '18 at 00:20
  • 1
    **HINT**: you can use Canvas and add in it whatever you wish to have in the screenshot. ultimately canvas is nothing but an image and then save the image in the file storage. – Arnold Parge Apr 26 '18 at 11:36

1 Answers1

5

I think this might be what you are looking for: https://docs.flutter.io/flutter/rendering/RenderRepaintBoundary/toImage.html

Basically you wrap your widget in a RepaintBoundary. Supply the RepaintBoundary with a key, which we can call boundaryKey. You can then do:

RenderRepaintBoundary boundary = boundaryKey.currentContext.findRenderObject();

and then do ui.Image image = await boundary.toImage(); to create an image that you can then use to create a png or whatever you want.

EduardKieser
  • 546
  • 1
  • 6
  • 11
  • Great solution ! I'm wondering if it is possible to capture only parts of the Widgets ? Because only a portion of the widget is what I needed. – colin Aug 17 '18 at 10:06
  • Perhaps you could define an invisable child widget that spans the are that you area interested in and wrap the repaint boundary around that? – EduardKieser Aug 17 '18 at 11:21