我正在构建一个颤振应用程序。该应用程序的用途是在 webview 中显示网页。在此应用程序中有一个按钮。单击按钮时,需要打开 webview 并加载 url。 但有些网址有重定向。所以在应用中我们不能回头。例如一个网址。 http://sample.com/firstpage.html。在普通浏览器中输入网址时。它将自动重定向到 http://sample.com/secondpage.html 。在浏览器中,当单击 secondarypage.html 中的后退按钮时。它不会返回到firstpage.html。但在我的网络视图中它正在循环。 当我在网页视图中加载 http://sample.com/firstpage.html 时。首先它会加载firstpage.html,然后它会自动重定向到secondpage.html。之后当我点击后退按钮时。它再次加载firstpagehtml并再次自动加载secondpage.html。 如何在不进入firstpage.html的情况下返回。它在普通浏览器中工作正常,但在网络视图中不起作用。怎么解决?
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(const MaterialApp(home: WebViewExample()));
class WebViewExample extends StatefulWidget {
const WebViewExample({super.key});
@override
State<WebViewExample> createState() => _WebViewExampleState();
}
class _WebViewExampleState extends State<WebViewExample> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context){ return WebViewExample1();}));
}, child: Text("open web view"))
],
),
);
}
}
class WebViewExample1 extends StatefulWidget {
const WebViewExample1({super.key});
@override
State<WebViewExample1> createState() => _WebViewExample1State();
}
class _WebViewExample1State extends State<WebViewExample1> {
late final WebViewController _controller;
@override
void initState() {
super.initState();
_loadPageFn2();
}
void _loadPageFn2(){
final WebViewController controller =WebViewController();
controller
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
debugPrint('WebView is loading (progress : $progress%)');
},
onPageStarted: (String url) {
debugPrint('Page started loading: $url');
},
onPageFinished: (String url) {
debugPrint('Page finished loading: $url');
},
onNavigationRequest: (NavigationRequest request) {
if (request.url.startsWith('https://www.youtube.com/')) {
debugPrint('blocking navigation to ${request.url}');
return NavigationDecision.prevent;
}
debugPrint('allowing navigation to ${request.url}');
return NavigationDecision.navigate;
},
onUrlChange: (UrlChange change) {
debugPrint('url change to ${change.url}');
},
),
)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
debugPrint('WebView is loading (progress : $progress%)');
},
onPageStarted: (String url) {
debugPrint('Page started loading: $url');
},
onPageFinished: (String url) {
debugPrint('Page finished loading: $url');
},
onWebResourceError: (WebResourceError error) {
debugPrint('''
Page resource error:
code: ${error.errorCode}
description: ${error.description}
errorType: ${error.errorType}
isForMainFrame: ${error.isForMainFrame}
''');
},
onNavigationRequest: (NavigationRequest request)async {
debugPrint('allowing navigation to ${request.url}');
return NavigationDecision.navigate;
},
onUrlChange: (UrlChange change) {
debugPrint('url change to ${change.url}');
},
),
)
..loadRequest(Uri.parse('http://sample.com/firstpage.html'));
_controller = controller;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
body: WillPopScope(
onWillPop: ()async{
if(await _controller.canGoBack()){
_controller.goBack();
return false;
} else{
return true;
}
},
child: WebViewWidget(controller: _controller),
),
);
}
}
这不是一个令人满意的答案:如果您根本不实现 onNavigationRequest 回调,则返回行为应该如您所期望的那样。
我认为这是 webview 库中的一个错误。