dart Flutter IOS向后滑动开启WillPop不工作

wxclj1h5  于 2023-03-10  发布在  Flutter
关注(0)|答案(2)|浏览(208)

我用WillPopScope Package 了我的支架,但是我的onWillPop在ios上不支持向后滑动,它只支持按前导,如何让onWillPop支持向后滑动?

return WillPopScope(
        onWillPop: () async {
          print('pop');
          Navigator.pop(context, product);
          setState(() {
            product.is_favorite = isFavorite;
          });
          return true;
        },
        child: Scaffold(
nbysray5

nbysray51#

当导航到当前页面时,您可以使用CupertinoPageRoute代替常规的MaterialPageRoute。

Navigator.of(context).push(
  CupertinoPageRoute(
    builder: (BuildContext context) {
      return WillPopScope(
        onWillPop: () async {
          print('pop');
          Navigator.pop(context, product);
          setState(() {
            product.is_favorite = isFavorite;
          });
          return true;
        },
        child: Scaffold(
          // Your Scaffold content here
        ),
      );
    },
  ),
);

CupertinoPageRoute,向后滑动手势将由iOS导航堆栈处理,该堆栈将依次调用WillPopScope小工具的onWillPop函数

ss2ws0br

ss2ws0br2#

答案是创建您自己的自定义小部件CustomPageRouteCustomWillPopScope并使用它们。

// or we can use just CupertinoPageRoute, it will works too.

class CustomPageRoute extends CupertinoPageRoute {
  @override
  @protected
  bool get hasScopedWillPopCallback {
    return false;
  }
  CustomPageRoute({
    required WidgetBuilder builder,
    RouteSettings? settings,
    bool maintainState = true,
    bool fullscreenDialog = false,
  }) : super(
    builder: builder,
    settings: settings,
    maintainState: maintainState,
    fullscreenDialog: fullscreenDialog,
  );
}
/////////////////////
class CustomWillPopScope extends StatelessWidget {
  const CustomWillPopScope(
      {required this.child,
      this.onWillPop = false,
      Key? key,
      required this.action})
      : super(key: key);

  final Widget child;
  final bool onWillPop;
  final VoidCallback action;

  @override
  Widget build(BuildContext context) {
    return Platform.isIOS
        ? GestureDetector(
            onPanEnd: (details) {
              if (details.velocity.pixelsPerSecond.dx < 0 ||
                  details.velocity.pixelsPerSecond.dx > 0) {
                if (onWillPop) {
                  action();
                }
              }
            },
            child: WillPopScope(
              onWillPop: () async {
                return false;
              },
              child: child,
            ))
        : WillPopScope(
            child: child,
            onWillPop: () async {
              action();
              return onWillPop;
            });
  }
}

当我们导航到一个页面时,在Navigator.push期间,我们使用CustomPageRoute,如下所示:

Navigator.push(
                      context,
                      CustomPageRoute(
                          builder: (context) => ProductScreen()));

然后在需要WillPopScope的页面上,我们也使用定制的WillPopScope,在Navigator.pop()中,我们将数据沿着上下文。

return
        CustomWillPopScope(
        action: () {
          print('pop');
          Navigator.pop(context, product);
          setState(() {
            product.is_favorite = isFavorite;
          });
        },
          onWillPop: true,
        child:
        Scaffold(

在我们完成所有这些步骤之后,WillPopScope将工作。

相关问题