为什么 WillPopScope 总是禁用向后滑动?
Why does WillPopScope always disable swipe backwards?
当按下新屏幕并想禁用向后滑动的功能时,我找到了 WillPopScope
小部件。
WillPopScope(
onWillPop: () async => false,
child: <some child>
然而,无论我是否 return true
或 false
,它都会阻止滑动手势。
是否可以告诉这个小部件它可以在某些屏幕状态下弹出?
如此有效我会:
WillPopScope(
onWillPop: () async => widget._canSwipeBack,
child: <some child>
现在我有 add/not-add 基于屏幕状态的小部件,这看起来很奇怪。
请参考以下代码
如果不使用 WillPopScope 包装小部件,通常会执行 Navigator.pop(context);
// disables swiping back
WillPopScope(
// disables swiping back or navigating back
onWillPop: () {},
child: Scaffold(
body: Container(),
),
);
WillPopScope(
onWillPop: () {
// whenever you want to navigate back to specific route
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
child: Scaffold(
body: Container(),
),
);
WillPopScope(
onWillPop: () {
// pop back
Navigator.pop(context);
},
child: Scaffold(
body: Container(),
),
);
当按下新屏幕并想禁用向后滑动的功能时,我找到了 WillPopScope
小部件。
WillPopScope(
onWillPop: () async => false,
child: <some child>
然而,无论我是否 return true
或 false
,它都会阻止滑动手势。
是否可以告诉这个小部件它可以在某些屏幕状态下弹出?
如此有效我会:
WillPopScope(
onWillPop: () async => widget._canSwipeBack,
child: <some child>
现在我有 add/not-add 基于屏幕状态的小部件,这看起来很奇怪。
请参考以下代码
如果不使用 WillPopScope 包装小部件,通常会执行 Navigator.pop(context);
// disables swiping back
WillPopScope(
// disables swiping back or navigating back
onWillPop: () {},
child: Scaffold(
body: Container(),
),
);
WillPopScope(
onWillPop: () {
// whenever you want to navigate back to specific route
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
child: Scaffold(
body: Container(),
),
);
WillPopScope(
onWillPop: () {
// pop back
Navigator.pop(context);
},
child: Scaffold(
body: Container(),
),
);