我创建了一个webviewscaffold,但是在从webview浏览时无法下载任何内容,但是当我单击按钮时,它什么也没做。我不知道从哪里开始,就像其他可以下载和预览的浏览器一样,我试图在这里实现相同的目的。
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();
}),
),
],
),
);
}
我希望,当我单击Web视图中的打印或下载按钮时,它应该像其他浏览器一样工作。
如果您是html页面的所有者,则可以检查对我有用的解决方法。在html页面的源代码中,为特定资源链接添加onclick函数:
function downloadpdf() {
var currentHref = window.location.href;
window.history.pushState(null, null, '/app/somepdf.pdf');
setTimeout(() => window.location.replace(currentHref), 1000);
}
在Flutter代码中添加侦听器:
StreamSubscription<String> _onWebViewUrlChanged;
_onWebViewUrlChanged =
FlutterWebviewPlugin().onUrlChanged.listen((String url) {
if (url.contains('.pdf')) {
launchURL(url);
}
});
launchURL将在外部浏览器窗口中打开预定义的URL。因此,您可以从flutter_webview_plugin下载pdf / etc。
您应该添加一些特定于应用的js / flutter魔术