Navigator PushReplacementName 在 flutter 中返回按钮
Navigator PushReplacementName gives back button in flutter
这很奇怪,但下面的代码对我不起作用。使用以下代码时,主屏幕上出现后退箭头。
下面第一行用于关闭对话框。第二个是转到主屏幕。
Navigator.of(context, rootNavigator: true).pop();
Navigator.of(context).pushReplacementNamed(HomeScreen.id);
这是我第一次遇到这种情况pushReplacementNamed
。这是怎么回事?
可能是因为堆栈中还有另一个屏幕。当您调用 pushReplacementNamed
时,它不会用您提供的堆栈替换整个堆栈。你能试试下面的代码吗;
// true 不基于上述查询条件
Navigator.of(context).pushNamedAndRemoveUntil(HomeScreen.id, (Route<dynamic> route) => true);
// 虚假作品
Navigator.of(context).pushNamedAndRemoveUntil(HomeScreen.id, (Route<dynamic> route) => false);
这不会给出所需的结果,因为您甚至在调用另一个 Navigator class 之前已经将上下文弹出。我尝试了 Navigator.of(context).pushNamedAndRemoveUntil()
函数,但仍然使用屏幕 1 上的后退按钮将我的 HomeScreen 压入堆栈两次。因此,我最终通过内置函数 Navigator.of(context).popUntil()
得到了这个。您可以运行这个dartpad代码https://dartpad.dev/a10ed43452736b5c6b3d1abe6a7eda45来查看想要的效果或者查看下面的代码。以下是要点中的部分代码:
...
class ThirdPage extends StatelessWidget{
static const routeName = '/third';
@override
Widget build(BuildContext context){
void _nextPage(){
//Logic here - ***************************
Navigator.of(context).popUntil((Route<dynamic> route) => route.isFirst);
}
return Scaffold(
appBar: AppBar(
title: Text('Third Page'),
),
body: Center(
child: Text('Third Page'),
),
floatingActionButton: FloatingActionButton(
onPressed: _nextPage,
child: Icon(Icons.add),
),
);
}
}
编码愉快 D:)
这很奇怪,但下面的代码对我不起作用。使用以下代码时,主屏幕上出现后退箭头。
下面第一行用于关闭对话框。第二个是转到主屏幕。
Navigator.of(context, rootNavigator: true).pop();
Navigator.of(context).pushReplacementNamed(HomeScreen.id);
这是我第一次遇到这种情况pushReplacementNamed
。这是怎么回事?
可能是因为堆栈中还有另一个屏幕。当您调用 pushReplacementNamed
时,它不会用您提供的堆栈替换整个堆栈。你能试试下面的代码吗;
// true 不基于上述查询条件
Navigator.of(context).pushNamedAndRemoveUntil(HomeScreen.id, (Route<dynamic> route) => true);
// 虚假作品
Navigator.of(context).pushNamedAndRemoveUntil(HomeScreen.id, (Route<dynamic> route) => false);
这不会给出所需的结果,因为您甚至在调用另一个 Navigator class 之前已经将上下文弹出。我尝试了 Navigator.of(context).pushNamedAndRemoveUntil()
函数,但仍然使用屏幕 1 上的后退按钮将我的 HomeScreen 压入堆栈两次。因此,我最终通过内置函数 Navigator.of(context).popUntil()
得到了这个。您可以运行这个dartpad代码https://dartpad.dev/a10ed43452736b5c6b3d1abe6a7eda45来查看想要的效果或者查看下面的代码。以下是要点中的部分代码:
...
class ThirdPage extends StatelessWidget{
static const routeName = '/third';
@override
Widget build(BuildContext context){
void _nextPage(){
//Logic here - ***************************
Navigator.of(context).popUntil((Route<dynamic> route) => route.isFirst);
}
return Scaffold(
appBar: AppBar(
title: Text('Third Page'),
),
body: Center(
child: Text('Third Page'),
),
floatingActionButton: FloatingActionButton(
onPressed: _nextPage,
child: Icon(Icons.add),
),
);
}
}
编码愉快 D:)