how to download/create pdf through webview in flutter

丶灬走出姿态 提交于 2020-05-14 18:07:11

问题


I have created a webviewscaffold but can't download anything from it while browsing from the webview, I made but when I click the button it does nothing. I don't know where to start, like in other browsers that can download and preview, I'm trying to achieve the same thing in here.

class AppState extends State<App> {


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Align(
            alignment: Alignment(1, 1),
            child: Container(
              child: Web(), // it is a statefulwidget that have WebviewScaffold, i have created it on a new page and imported/used here
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(top: 20.0),
              )
            ],
          ),
          Align(
            alignment: Alignment(0.7, -0.93),
            child: FloatingActionButton(
                tooltip: "Share",
                child: Icon(
                  Icons.share,
                  color: Colors.amberAccent,
                ),
                onPressed: () {
                  _onShareTap();
                }),
          ),
        ],
      ),
    );
  }

I expect, when I click the print or download button within the webview it should work like any other browser.


回答1:


If you are the owner of html page, you can check workaround that works for me. In source of your html page add onclick function for specific resource links:

function downloadpdf() {
      var currentHref = window.location.href;
      window.history.pushState(null, null, '/app/somepdf.pdf');
      setTimeout(() => window.location.replace(currentHref), 1000);
}

In Flutter code add listener:

StreamSubscription<String> _onWebViewUrlChanged;
_onWebViewUrlChanged =
    FlutterWebviewPlugin().onUrlChanged.listen((String url) {
      if (url.contains('.pdf')) {
        launchURL(url);
      }
});

launchURL will open predefined url in external browser window. So you can download pdf/etc from flutter_webview_plugin.

You should add some your app-specific js/flutter magic



来源:https://stackoverflow.com/questions/56247542/how-to-download-create-pdf-through-webview-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!