简体   繁体   English

如何在 Flutter_webview 中显示或下载 pdf 文件?

[英]How can I show or download pdf files inside a Flutter_webview?

I want to open or download a PDF-File inside my webview.我想在我的 webview 中打开或下载 PDF 文件。 I tried to enableFilesURl inside the webview community version but it doesn´t work.我尝试在 webview 社区版本中启用FilesURl,但它不起作用。

Best regards Falk最好的问候福尔克

Future<File> createFileOfPdfUrl({String urlP = ''}) async {
                            final url = urlP;
                            String filename = '${DateTime.now().microsecondsSinceEpoch}';
                            var request = await HttpClient()?.getUrl(Uri.parse(url));
                            var response = await request.close();
                            var bytes = await consolidateHttpClientResponseBytes(response);
                            String dir = (await getApplicationDocumentsDirectory()).path;

                            File file = new File('$dir/$filename');
                            await file.writeAsBytes(bytes);
                            return file;
                          }

. . . .

createFileOfPdfUrl(urlP: '${jsonRes['m']}').then((res) {
                                    Navigator.push(context, MaterialPageRoute(builder: (context) => PDFScreen(res.path)));
                                  });

. . . . . .

class PDFScreen extends StatelessWidget {
  String pathPDF = "";

  PDFScreen(this.pathPDF);

  @override
  Widget build(BuildContext context) {

    return PDFViewerScaffold(
        appBar: AppBar(
          leading: IconButton(
            onPressed: () {
              Navigator.pop(context);
            },
            icon: Icon(Icons.arrow_back_ios),
          ),
          centerTitle: true,
          title: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Text(
                "PDF",
                style: TextStyle(
                  fontSize: 16,
                  color: Theme.of(context).accentColor,
                ),
              ),
              Text("Önizleme", style: TextStyle(color: Theme.of(context).accentTextTheme.subtitle.color, fontSize: 14)),
            ],
          ),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.share),
              onPressed: () {
                Share.share('$pathPDF');
              },
            ),
          ],
        ),
        path: pathPDF);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM