首页 > 解决方案 > 在 webview 中颤动水平滑动手势

问题描述

我现在正在构建一个 Flutter 应用程序,它有一个登录名,它路由到一个新屏幕,其中包含一个脚手架,其中包含一个 webView 作为主体。对于我使用的导航

Navigator.pushNamed(
  context,
  webshopRoute,
  arguments: WebshopArguments(initialUrl: shopUrl),
);

当我在 webView 中浏览时,我想返回 - 在 webView 中 - 使用从右到左的滑动手势(对于没有像我的 Pixel 4 这样的后退按钮的手机)。但是现在滑动所做的是它跳回登录屏幕。

我在这里找到的唯一帖子如何禁用它,这是我不想要的。

非常感谢!

标签: flutterdart

解决方案


好的,所以诀窍是用 WillPopScope 包装脚手架并在 onWillPop 内部的控制器上调用 goBack()。

late WebViewController _webViewController;

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        _webViewController.goBack();
        return false;
      },
      child: Scaffold(
        body: SafeArea(
          child: WebView(javascriptMode: JavascriptMode.unrestricted,
                 onWebViewCreated: (WebViewController webViewController) {
                 _webViewController = webViewController;
                 },
.....

推荐阅读