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

时间:2019-10-17 11:44:28

标签: android ios flutter mobile dart

我想在我的Web视图中打开或下载PDF文件。我试图在webview社区版本中启用enableFilesURl,但是它不起作用。

最好的问候 福尔克

1 个答案:

答案 0 :(得分:0)

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);
  }
}