0

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(),
          ],
        ),
      ),
    );
Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
  • Hi, try changing the return fasle; to return Future.value(false); – Gwhyyy Nov 09 '22 at 13:37
  • if it's not the problem, please check this question it seems it have the answer for you https://stackoverflow.com/questions/57878887/using-flutter-webview-as-home-and-pressing-back-button-closes-application – Gwhyyy Nov 09 '22 at 13:37

1 Answers1

0

try this also:

Future<bool> _x(){
 if(canGoBack()){
     _controller.goBack():
     return Future.value(false):
 }else{
      return Future.value(true): // this will pop the widget
 }

}

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35