Mir*_*lle 6 flutter flutter-inappwebview
我是 Flutter 新手,我想在其他小部件之间放置一个 Web 视图,并且我正在使用 InAppWebView flutter 来做到这一点,但我想阻止用户在单击按钮时导航到其他页面在网络视图内。我是否必须为 iOS 和 Android 编写一些本机代码来阻止导航?
谢谢
Moa*_*waf 11
由于您使用的是InAppWebView
包,因此您可以拦截 Webview 在实际导航之前导航到的每个 URL,然后决定是否使用回调进行导航,shouldOverrideUrlLoading
如下所示:
shouldOverrideUrlLoading: (controller, navigationAction) async {
// You can access the URL like this
final url = navigationAction.request.url.toString();
if (condition) {
// This one means do not navigate
return NavigationActionPolicy.CANCEL;
}
// This one means navigate
return NavigationActionPolicy.ALLOW;
},
Run Code Online (Sandbox Code Playgroud)
注意:要使用回调shouldOverrideUrlLoading
,您必须在initialOptions
Web 视图中启用它,如下所示,因为默认值是false
:
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
),
),
Run Code Online (Sandbox Code Playgroud)
小智 6
像这样的东西:
InAppWebView(
....
shouldOverrideUrlLoading: (controller, navigationAction) async {
final uri = navigationAction.request.url!;
if(whitenAvigationHosts.contains(uri.host)){
return NavigationActionPolicy.ALLOW;
}
return NavigationActionPolicy.CANCEL;
})
Run Code Online (Sandbox Code Playgroud)
您可以通过导航委托属性阻止 Web 视图中的导航,如下所示:
WebView(
debuggingEnabled: true,
initialUrl: _currentURL,
onWebViewCreated: (controller) {
_controller = controller;
return _controller;
},
onPageFinished: (url) {
if (url.contains(ConstUtils.course)) {
updateProfile();
}
},
navigationDelegate: (navigation) => NavigationDecision.prevent,
onWebResourceError: (error) {
NavigationUtils(context).popCurrentPage();
},
onPageStarted: (url) {
},
javascriptMode: JavascriptMode.unrestricted,
),
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12653 次 |
最近记录: |