I'm building a small web view app, here I'm checking is there is a back history then it will go the previous page using WilloScope else close the app. My app goes back page if there is history but not closing the app if not.
Future<bool> initBackButton() async {
if (_controller!.canGoBack() != null) {
_controller?.goBack();
} else {
SystemNavigator.pop();
}
return false;
}
return Scaffold(
body: SafeArea(
child: Stack(
children: <Widget>[
WillPopScope(
onWillPop: () => initBackButton(),
child: WebView(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: 'https://google.com',
onWebViewCreated: (controller) => _controller = controller,
navigationDelegate: (NavigationRequest request) {
setState(() {
isLoading = true;
});
return NavigationDecision.navigate;
},
onPageFinished: (String url) {
setState(() {
isLoading = false;
});
},
// onPageFinished: (finish) {
// setState(() {
// var isLoading = false;
// });
// },
),
),
isLoading
? const Center(
child: CircularProgressIndicator(),
)
: Stack(),
],
),
),
);