如何在颤动中处理后退时的切换页面逻辑
how to handle switch page logic on back press in flutter
我是flutter的新手,我通过onWillPop() 返回了按下事件以显示警告对话框。但我只需要显示主页的警报对话框(比如你想退出吗?)。在代码中,如果当前页面是主页,我有一个类似当前页面的类型,然后只显示警报对话框或切换到主页。
代码
if(currentType == Home)
{
showExitDialog();
}
else
{
switchToHome();
}
我在哪里可以处理这个?
在您的应用栏上设置自定义 leading
属性。
默认值是后退一页,所以如果你什么都不设置,后退箭头会在那里,它会Navigator.of(context).maybePop();
来自 Navigator 文档
If this is null and [automaticallyImplyLeading] is set to true, the [AppBar] will imply an appropriate widget. For example, if the [AppBar] is in a [Scaffold] that also has a [Drawer], the [Scaffold] will fill this widget with an [IconButton] that opens the drawer (using [Icons.menu]). If there's no [Drawer] and the parent [Navigator] can go back, the [AppBar] will use a [BackButton] that calls [Navigator.maybePop].
但如果您想覆盖它,请执行此操作
在您的主页上
appBar: AppBar(
leading: IconButton(icon: Icon(Icons.exit_to_app),onPressed: (){
exitTheApp();
},),
),
在您的其他 AppBar 上
appBar: AppBar(
leading: IconButton(icon: Icon(Icons.home),onPressed: (){
showHomePage();
},),
),
我是flutter的新手,我通过onWillPop() 返回了按下事件以显示警告对话框。但我只需要显示主页的警报对话框(比如你想退出吗?)。在代码中,如果当前页面是主页,我有一个类似当前页面的类型,然后只显示警报对话框或切换到主页。
代码
if(currentType == Home)
{
showExitDialog();
}
else
{
switchToHome();
}
我在哪里可以处理这个?
在您的应用栏上设置自定义 leading
属性。
默认值是后退一页,所以如果你什么都不设置,后退箭头会在那里,它会Navigator.of(context).maybePop();
来自 Navigator 文档
If this is null and [automaticallyImplyLeading] is set to true, the [AppBar] will imply an appropriate widget. For example, if the [AppBar] is in a [Scaffold] that also has a [Drawer], the [Scaffold] will fill this widget with an [IconButton] that opens the drawer (using [Icons.menu]). If there's no [Drawer] and the parent [Navigator] can go back, the [AppBar] will use a [BackButton] that calls [Navigator.maybePop].
但如果您想覆盖它,请执行此操作
在您的主页上
appBar: AppBar(
leading: IconButton(icon: Icon(Icons.exit_to_app),onPressed: (){
exitTheApp();
},),
),
在您的其他 AppBar 上
appBar: AppBar(
leading: IconButton(icon: Icon(Icons.home),onPressed: (){
showHomePage();
},),
),