kamranbekirovyz
kamranbekirovyz

Reputation: 1321

How to take screenshot in Flutter?

Is there a way or package that can help either taking fullscreen screenshot, or screenshot of widget that is wrapped or at least sharing the picture of screen via native share option?

There are some packages and I have tried, did not found any useful one.

Upvotes: 2

Views: 3275

Answers (1)

Mariano Zorrilla
Mariano Zorrilla

Reputation: 7666

RepaintBoundary is the Widget you're looking for, this one can be converted into an image.

Example:

Future<CaptureResult> captureImage() async {
    final pixelRatio = MediaQuery.of(context).devicePixelRatio;
    final boundary = _boundaryKey.currentContext.findRenderObject() as RenderRepaintBoundary;
    final image = await boundary.toImage(pixelRatio: pixelRatio);
    final data = await image.toByteData(format: ui.ImageByteFormat.png);
    return CaptureResult(data.buffer.asUint8List(), image.width, image.height);
}

final _boundaryKey = GlobalKey();

RepaintBoundary(
   key: _boundaryKey,
   child: Container(),// Your Widgets to be captured.
)

Link: capture_widget.dart

Upvotes: 3

Related Questions