Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Screenshot of current webview #181

Closed
marianoarga opened this issue Sep 6, 2018 · 7 comments
Closed

Screenshot of current webview #181

marianoarga opened this issue Sep 6, 2018 · 7 comments

Comments

@marianoarga
Copy link

Using RenderRepaintBoundary tecnique to take a screenshot of the webview returns an image with the CircularProgressIndicator widget instead of the already loaded URL, do you have an alternative for this? maybe RenderRepaintBoundary is relading the widget before taking the actual "canvas" image?

takescrshot() async {
RenderRepaintBoundary boundary = scr.currentContext.findRenderObject();
var image = await boundary.toImage();
var byteData = await image.toByteData(format: ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
print(pngBytes);
}

@erdemyerebasmaz
Copy link

@marianoarga Have you found a solution to this problem?

@marianoarga
Copy link
Author

@erdemyerebasmaz No, I continue without screenshot feature.

@charafau
Copy link
Collaborator

it's not posible to take a screenshot of webview, because of the way plugin works - it's native android activity with flutter view on top.

I dont know what your use case is, but you need to fallback to native code of given platform to take screenshot.

Therefore, it's not possible with plugin now, closing.

@erdemyerebasmaz
Copy link

@charafau I'd like to show a dialog with webview visible on background, afaik this is not possible at the moment. That's why I wanted to show webview's screenshot as a widget and hide the webview till the action from dialog is complete.
I will inject an external library to capture screenshot, convert the image back to raw data and probably send it via a post message back to flutter app.

@charafau
Copy link
Collaborator

@erdemyerebasmaz I don't know your use-case but I think it's out of scope from this project. I think I should add it in readme, this webview was created to have something to authenticate with oauth2, display simple webpages, until Google will make their own main webview.

@erdemyerebasmaz
Copy link

erdemyerebasmaz commented Jun 13, 2019

For those who still want to take screenshot of the visible webview and pass it back to Flutter on Android:

I solved this issue in this PR of a forked repository.
I forgot to import ByteArrayOutputStreamclass to WebviewManager, please add the following to WebviewManager
import java.io.ByteArrayOutputStream;

! Please note that you need to fork the plugin and add these changes, this is not supported by the official plugin as it is out of it's scope !

For our use case, we needed to hide webview to interact with an AlertDialog and show webview's screenshot in the background.

Below is a brief and incomplete example on how to use it on your Flutter app, it has code bits that you might never need that is related to our use case.
Pseudocode:

final _webview = new FlutterWebviewPlugin();
Uint8List  _screenshotData;

//_getScreenshot is just a local function on our Flutter app, don't confuse it with plugins takeScreenshot() below
Future _getScreenshot() async {
   // This is where we take the screenshot with plugin, 
    Uint8List _imageData = await _webview.takeScreenshot(); 
    setState(() {
      _screenshotData = _imageData;
    });
  }

//Call this where you need the screenshot:
_getScreenshot().whenComplete(() {
    // Wait for memory image to load to avoid render issues
     Timer(Duration(milliseconds: 100), () {
    // For our use case, we needed to hide Webview to interact with an AlertDialog
        _webview.hide();
    });
});

WebviewScaffold(
...
    initialChild: _screenshotData != null ? Image.memory(_screenshotData) : (Your Placeholder Widget),
...
);

@marianoarga Please check if you still need this feature.

@marianoarga
Copy link
Author

@erdemyerebasmaz Thanks for your effort <3, unfortunately I'm kinda out of this app by the moment, but I'm sure this will help many developers using webviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants