我创建了一个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视图中的“打印”或“下载”按钮时,它应该像其他浏览器一样工作。
答案 0 :(得分:3)
您可以使用我的插件flutter_inappwebview,它是Flutter插件,允许您添加内联WebView或打开应用程序内浏览器窗口,并且具有许多事件,方法和选项来控制WebView。 / p>
要识别可下载的文件,您需要设置useOnDownloadStart: true
选项,然后才能收听onDownloadStart
事件!
例如,在Android上,您还需要在AndroidManifest.xml
文件中添加写权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
然后,您需要使用permission_handler插件请求权限。相反,要有效下载文件,您可以使用flutter_downloader插件。
以下是使用http://ovh.net/files/(特别是http://ovh.net/files/1Mio.dat作为URL)来测试下载的完整示例:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterDownloader.initialize(
debug: true // optional: set false to disable printing logs to console
);
await Permission.storage.request();
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
InAppWebViewController webView;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('InAppWebView Example'),
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: InAppWebView(
initialUrl: "http://ovh.net/files/1Mio.dat",
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
debuggingEnabled: true,
useOnDownloadStart: true
),
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
onDownloadStart: (controller, url) async {
print("onDownloadStart $url");
final taskId = await FlutterDownloader.enqueue(
url: url,
savedDir: (await getExternalStorageDirectory()).path,
showNotification: true, // show download progress in status bar (for Android)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);
},
))
])),
),
);
}
}
在这里,如您所见,我也在使用path_provider插件来获取要保存文件的文件夹。
答案 1 :(得分:1)
如果您是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魔术