Home > Software design >  how to take screenshot of specific area on the screen in flutter?
how to take screenshot of specific area on the screen in flutter?

Time:11-17

enter image description here

I want to extract an image of a specific area on the screen? I have the x, y, height, and width. Anybody knows how can I achieve that?

CodePudding user response:

well i don't know about x and y but what you can do is screen shot of specific widgets with package screenshot, link

this package capture widgets as Images, even if they are not rendered on screen.

Step 1: create a controller

ScreenshotController screenshotController = ScreenshotController();

Step 2: wrap widgets with Screenshot()

Screenshot(
    controller: screenshotController,
    child: Text("This text will be captured as image"),
),

step 3: Take the screenshot by calling capture method. This will return a Uint8List

screenshotController.capture().then((Uint8List image) {
    //Capture Done
    setState(() {
        _imageFile = image;
    });
}).catchError((onError) {
    print(onError);
});

CodePudding user response:

Through RepaintBoundary widget you can take screenshot of specific area which you want. I put one example at here which I performed in my project. Please go with it. I hope it will helpful to you surely.

Declare globalkey first...

 GlobalKey globalKey = GlobalKey();

Then wrap your widget with RepaintBoundary like below with use of this globalKey.

 RepaintBoundary(
            key: globalKey,
            child: Obx(() {
              return Container(
                decoration: BoxDecoration(image: DecorationImage(image: NetworkImage(mainController.imageName.value))),
                height: 390,
                width: 390,
                child: Stack(
                  children: generatedNameWidgets,
                ),
              );
            })),
  • Related