I wrote the code by wrapping willpopScope so that I can get previous page of webview but it is not working thus it is closing the entire webView page.
Here is the full code:
class Resources extends StatefulWidget {
@override
_ResourcesState createState() => _ResourcesState();
}
class _ResourcesState extends State<Resources> {
late WebViewController _controller;
final Completer<WebViewController> _controllerCompleter =
Completer<WebViewController>();
Future<bool> pop(BuildContext context) async {
if (await _controller.canGoBack()) {
_controller.goBack();
return Future.value(false);
} else {
return Future.value(true);
}
}
@override
void initState() {
super.initState();
if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: WillPopScope(
onWillPop:()=> pop(context),
child: Scaffold(
appBar: AppBar(),
body: WebView(
gestureNavigationEnabled: true,
initialUrl: 'https://www.edhitch.com/login.html',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controllerCompleter.future.then((value) => _controller = value);
_controllerCompleter.complete(webViewController);
},
),
),
),
),
);
}
}
I have used will scope which takes bool function pop kindly help me out with it.
Try to below code hope it helps to you:
Create Future function using async and it will return true
Future<bool> onWillPopFunction() async {
return true;
}
and call your onWillPopFunction, function on onWillPop event
WillPopScope(
child: Scaffold(),
onWillPop: onWillPopFunction,
)
you also add alert box for warning like (exit your app or not?)
Or try below code
IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.pop(context, false),
),
You need to set the controller when the WebView is created and pop for the controller, not the app context:
onWillPop: (){
_controller.goBack();
},
(updated, since "Controller.complete()" does not exist anymore)
onWebViewCreated: (WebViewController webViewController) {
_controller = webViewController;
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.