Search code examples
androidfluttergestureflutter-inappwebview

Back gesture in Flutter inAppWebView not working on Android


My inAppWebView recognises the Back and Forward gestures (swipe in from left/right edge) fine on iOS and automatically performs the goBack/goForward accordingly (no code added for that).

But on Android it simply does nothing – it does not react to swiping in from left/right edge at all.

Is there anything one needs to do / set up to make this work also on Android?

My structure is: SafeArea -> Scaffold -> Container -> Expanded -> Container -> InAppWebView

No explicit or custom Gesture Recognizers anywhere.

I tried to use the inAppWebView property "gestureRecognizers" - but without any effect. Whatever gesture recognizers I drop in there, the inAppWebView behaves exactly the same. So that property to me seems to do nothing at all.


Solution

  • android:windowSwipeToDismiss="true"

    In AndroidManifest.xml add the following attribute to your application tag:

    Code:

    class MyInAppWebView extends StatefulWidget {
      @override
      _MyInAppWebViewState createState() => _MyInAppWebViewState();
    }
    
    class _MyInAppWebViewState extends State<MyInAppWebView> {
      late InAppWebViewController _webViewController;
      bool _canGoBack = false;
    
      @override
      Widget build(BuildContext context) {
        return WillPopScope(
          onWillPop: () async {
            if (_canGoBack) {
              _webViewController.goBack();
              return false;
            }
            return true;
          },
          child: Scaffold(
            appBar: AppBar(title: Text('InAppWebView')),
            body: InAppWebView(
              initialUrlRequest: URLRequest(url: Uri.parse('https://www.youtube.com')),
              onWebViewCreated: (controller) {
                _webViewController = controller;
              },
              onLoadStop: (controller, url) {
                setState(() {
                  _canGoBack = true;
                });
              },
            ),
          ),
        );
      }
    }