簡體   English   中英

"Flutter iOS Back Swipe 不調用 onWillPop"

[英]Flutter iOS Back Swipe does not call onWillPop

我用WillPopScope覆蓋了我的Scaffold ,但是在滑動返回 iOS 時不會調用onWillPop回調。
這可能是什么原因?

@override
Widget build(BuildContext context) {
  return WillPopScope(
    onWillPop: () async {
      print("onWillPop"); // is not printed to the console
      if (Navigator.of(context).userGestureInProgress)
        return false;
      else
        return true;
    },
    child: new Scaffold(
      appBar: new AppBar(
        title: new Text("App Title"),
      ),
      body: Stack(
        children: <Widget>[
          ...widgets...
        ],
      ),
    ),
  );
}

我找到了原因:WillPopScope 不起作用,因為上面構建方法的widget 不是top widget(main 調用的widget)。 我希望它可以幫助其他人。

這是一個問題,在這個問題中討論了很多,但有一種方法可以解決它,那就是使用這個包

return ConditionalWillPopScope(
child: _MyScreenContent(),
onWillPop: _onWillPop,
shouldAddCallbacks: _hasChanges,
 );

正如本文所提到這里<\/a>,讓你的工作WillPopScope,<\/strong>你應該重寫hasScopedWillPopCallback<\/strong>吸氣劑MaterialPageRoute<\/strong>這樣的:

class CustomMaterialPageRoute extends MaterialPageRoute {
  @protected
  bool get hasScopedWillPopCallback {
    return false;
  }
  CustomMaterialPageRoute({
    @required WidgetBuilder builder,
    RouteSettings settings,
    bool maintainState = true,
    bool fullscreenDialog = false,
  }) : super(
          builder: builder,
          settings: settings,
          maintainState: maintainState,
          fullscreenDialog: fullscreenDialog,
        );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM