flutter inAppWebview:响应点击 pdf link
flutter inAppWebview: reacting to click on pdf link
我正在尝试在 flutter 中使用 InAppWebview 插件,效果很好,但是当我点击 pdf 时 link 它什么也没做。
尝试点击 google 搜索中的第一个或第二个 link。
或shouldOverrideUrlLoading
或onDownloadStart
关于如何显示或如何拦截它有什么想法吗?
class WebViewWidget extends StatefulWidget {
final String url;
WebViewWidget(this.url);
@override
_WebViewWidgetState createState() => _WebViewWidgetState();
}
class _WebViewWidgetState extends State<WebViewWidget> {
final Completer<InAppWebViewController> _controller =
Completer<InAppWebViewController>();
final InAppWebViewGroupOptions _options = InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false,
javaScriptEnabled: true,
),
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
loadWithOverviewMode: true,
useWideViewPort: false,
builtInZoomControls: false,
domStorageEnabled: true,
supportMultipleWindows: true,
),
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Builder(builder: (BuildContext context) {
return InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse("https://www.google.com/search?client=firefox-b-d&q=pdf+example")),
initialOptions: _options,
shouldOverrideUrlLoading: (controller, action) {
print("override");
return Future.value(NavigationActionPolicy.ALLOW);
},
onWebViewCreated: (webViewController) {
_controller.complete(webViewController);
},
onDownloadStart: (controller, uri) {
print("download");
},
);
}),
);
}
}
要调用onDownloadStart
必须在crossPlatform: InAppWebViewOptions()
上设置useOnDownloadStart: true
。一旦完成,回调就会被正确调用。
我正在尝试在 flutter 中使用 InAppWebview 插件,效果很好,但是当我点击 pdf 时 link 它什么也没做。
尝试点击 google 搜索中的第一个或第二个 link。
或shouldOverrideUrlLoading
或onDownloadStart
关于如何显示或如何拦截它有什么想法吗?
class WebViewWidget extends StatefulWidget {
final String url;
WebViewWidget(this.url);
@override
_WebViewWidgetState createState() => _WebViewWidgetState();
}
class _WebViewWidgetState extends State<WebViewWidget> {
final Completer<InAppWebViewController> _controller =
Completer<InAppWebViewController>();
final InAppWebViewGroupOptions _options = InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false,
javaScriptEnabled: true,
),
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
loadWithOverviewMode: true,
useWideViewPort: false,
builtInZoomControls: false,
domStorageEnabled: true,
supportMultipleWindows: true,
),
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Builder(builder: (BuildContext context) {
return InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse("https://www.google.com/search?client=firefox-b-d&q=pdf+example")),
initialOptions: _options,
shouldOverrideUrlLoading: (controller, action) {
print("override");
return Future.value(NavigationActionPolicy.ALLOW);
},
onWebViewCreated: (webViewController) {
_controller.complete(webViewController);
},
onDownloadStart: (controller, uri) {
print("download");
},
);
}),
);
}
}
要调用onDownloadStart
必须在crossPlatform: InAppWebViewOptions()
上设置useOnDownloadStart: true
。一旦完成,回调就会被正确调用。