颤动: 移动到一个新的屏幕而不提供导航回到上一个屏幕

我在我的 Flutter 应用程序中实现了一个认证流程。

在尝试签名之后,CheckAuth (它检查用户是否登录,然后相应地打开主屏幕或登录屏幕)用以下代码打开:

  void _signIn() async {
await _auth
.signInWithEmailAndPassword(
email: _userEmail.trim(), password: _userPassword.trim())
.then((task) {
// go to home screen
if (task.getIdToken() != null) {
setState(() {
Navigator.pushReplacement(
context,
new MaterialPageRoute(
builder: (BuildContext context) => new CheckAuth()));
});
} else {
print("Authentication failed");
}
});
}

问题: 我可以成功地登录到应用程序,但如果我点击后退按钮,我登录后,它回到屏幕上的签名(当我期望它退出应用程序)。

问题: 在 Flutter,如何在没有回头路的情况下从一个屏幕移动到另一个屏幕?

我需要删除导航历史吗?还是根本不用导航器?我尝试了 导航员,替换方法,但似乎不起作用。

101171 次浏览

You need to use Navigator.pushReplacement when leaving the auth screen too. Not just when redirecting to login page.

You need to use

Navigator
.of(_context)
.pushReplacement(new MaterialPageRoute(builder: (BuildContext context) => page));

Where _context is object of BuildContext And page is which page you directed to.

You can use the pushAndRemoveUntil method:

Push the given route onto the navigator that most tightly encloses the given context, and then remove all the previous routes until the predicate returns true. To remove all the routes below the pushed route, use a [RoutePredicate] that always returns false (e.g. (Route<dynamic> route) => false).

Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => MainPage()),
(Route<dynamic> route) => false,
);

We can use routes for the same Like:

routes: {


LoginScreen.route_name: (_) => LoginScreen(),
.....
},

And use below code whenever you want to push and remove the backstack

Navigator.of(context).pushReplacementNamed(LoginScreen.route_name);

Note: Define static String inside widget LoginScreen

I think you probably have already solved this. But you can set "automaticallyLeadingImplied: false" in the AppBar of the Scaffold you are navigating to.

I have resolved this by popping from current page and showing new page:

Navigator.pop(context);
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => newPage));

Just simply add the below code:

Navigator.of(context).pushNamedAndRemoveUntil('/routeName', (route) => false);

if you are working with Getx state managemaent then you can try this

Get.of(()=> NewPage());

For anyone wondering there is a new argument that needs to be returned false Navigator.of(context).pushNamedAndRemoveUntil('/', (route) => false);

Navigator.of(context, rootNavigator: true).pop();
Navigator.pushNamed(context, '/');

Here is the solution -

Use pushAndRemoveUntil instead of pushReplacement

Also, can be use maintainState: true

For set root page

Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => pageName, maintainState: true),
(Route<dynamic> route) => false);

For Push Page one page to another

Navigator.push(
context,
MaterialPageRoute(
builder: (context) => pageName,
maintainState: false),)

**If you want to refresh always while appearing page then use: **

maintainState: false

It was not working for me because I was using the home proptery rather than initialRoute on the MaterialApp.

This is a link where there's the folloing warning which helped me to spot the error: https://docs.flutter.dev/cookbook/navigation/named-routes#2-define-the-routes

Warning: When using initialRoute, don’t define a home property.