0

I want my app when install first thing create specific directory in internal storage and a button when clicked it's convert specific widget(screen) to image.png then save it in that created directory.

I need full code for that. I've been looking, but I haven't found an effective way.

mhmd
  • 395
  • 4
  • 19
  • Does this answer your question? [How to take a screenshot of the current widget - Flutter](https://stackoverflow.com/questions/51117958/how-to-take-a-screenshot-of-the-current-widget-flutter) – Jim Oct 26 '21 at 02:51

1 Answers1

0

this function is all your need for screenshot a widget and save locally:

static GlobalKey _repaintKey = GlobalKey();

Widget _yourWidget(){
  return Stack(
    key: _repaintKey,
    children: [
    ],
  );
}

Future<void> _takeScreenShot(context) async {
    RenderRepaintBoundary boundary = _repaintKey.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    final path = join(
      (await getTemporaryDirectory()).path, "screenshot${DateTime.now().toIso8601String()}.png");
    File imgFile = File(path);
    imgFile.writeAsBytes(pngBytes).then((value) {
      Navigator.of(context).pushNamed(Routes.uploadImage, arguments: value.uri.path);
    }).catchError((onError) {
      print(onError);
    });
}
Jim
  • 6,928
  • 1
  • 7
  • 18