Flutter back-pressed shows black screen - flutter

I have created NavigationDrawer in flutter before navigation drawer screen appear to user I have 3 screen before it
splash screen
intro slider
Login screen
NavigationDrawer
I want to close my app when user press back button in android phone from navigationdrawer screen but it will shows black screen.
I have called below called for navigation from login to drawer
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (BuildContext context) => NavigationDrawerDemo(),
),
ModalRoute.withName('/LoginFieldForm'));
I called loginfieldform with
Navigator.push(context,
new MaterialPageRoute(builder: (context) =>new LoginFieldForm()));

I think you have pushed the "LoginFieldForm" with the Navigator.push method and trying to remove it using the named routes.
For Ex :
// Puching a route with Navigator.push method
Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginFieldForm()),
);
// Removing using the named routes.
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (BuildContext context) => NavigationDrawerDemo(),
),
ModalRoute.withName('/LoginFieldForm'));
You can only use ModalRoute.withName() if you have added the route using named routes.

Your route probably has not a name set. If so, popping until the name is matched will result in popping everything, since the name you're looking for will never be found.
When you push LoginFieldForm ensure to pass a settings to the Route.
When pushing, you can do this:
MaterialPageRoute(
settings: RouteSettings(name: "routeName"),
builder: (context) => YourWidget(),
)
If you're within onGenerateRoute you already have settings passed as an argument to the onGenerateRoute function. In that case, just forward them to the MaterialPageRoute:
MaterialPageRoute(
settings: settings, //these settings are arguments from the function
builder: (context) => YourWidget(),
)

To close the app you may use exit(0) of dart:io on back press.exit(0) help out to Exit the Dart VM process immediately with the given exit code.
This does not wait for any asynchronous operations to terminate. You may find more information about exit(0) here
Or
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
For more detail you may check here

Use-case: pushNamedAndRemoveUntil
Navigator.of(context).pushNamedAndRemoveUntil('/LoginFieldForm',
(Route<dynamic> route) => false);

Related

How to switch the screen smoothly?

I have three screens: 1, 2, and 3.
I want to switch from screen 2 to screen 3, but when I pop screen 2 and push screen 3, then it shows screen 1 for a while, then it shows screen 3, so is there any way to switch screens using pop after push or not?
this code not expectations
Navigator.push(
context,
MaterialPageRoute<Widget>(
builder: (context) => widget,
),
);
Navigator.pop(context);
Using pushReplacement insted of push
Navigator.pushReplacement(
context,
MaterialPageRoute<Widget>(
builder: (context) => widget,
),
);
Using pushReplacement insted of push
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => widget,
),
);
The behavior you're describing is likely because when you call Navigator.pop(context), it removes the current screen (screen 2) from the navigation stack, but the framework hasn't yet had a chance to build and display the new screen (screen 3) that you pushed. So, for a brief moment, the previous screen (screen 1) is visible.
One way to switch screens without that flicker is to use Navigator.pushReplacement instead of Navigator.push followed by Navigator.pop. Navigator.pushReplacement removes the current screen from the navigation stack and replaces it with the new screen, all in one step.
Example:
Navigator.pushReplacement(
context,
MaterialPageRoute<Widget>(
builder: (context) => widget3,
),
);
This will directly switch from screen 2 to screen 3.
It is generally not recommended to use both Navigator.push() and Navigator.pop() together in the same function call, as this can cause unexpected behavior. Instead, you can use Navigator.replace() to replace the current screen with a new one, or use Navigator.pushNamedAndRemoveUntil() to push a new screen and remove all the screens that come before it.

How to remove backstack including home page in flutter

I have seen many examples but none of them providing me a way to remove the entire back stack(including the home page) while navigating to the next page.
Eg: I have a few login pages once a user successfully entered login credentials user move to the home screen, so here I want to remove all previous screen which appeared till now, How can I do that?
Currently using code:
Navigator.of(context).pushNamedAndRemoveUntil(
HomeScreen.route_name, ModalRoute.withName('/'));
To remove all the routes below the pushed route, use a RoutePredicate that always returns false (e.g. (Route route) => false).
So for your code to work as expected, remove ModalRoute.withName('/') and give a route predicate which returns false. So it should be
Navigator.of(context).pushNamedAndRemoveUntil(
HomeScreen.route_name, (Route<dynamic> route)=>false);
For reference see the official documentation
Try this out. This code push the given route onto the navigator and then remove all the previous routes even HomeScreen.
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (BuildContext context) => HomeScreen(),
),
(route) => false,
);
Note: This is tested on my project. So I hope it will 100% work
Try this way
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (dialogContex) => HomePageScreen()),
ModalRoute.withName("/HomePageScreen"));
or with Getx:
Get.offNamedUntil('home', (route) => false);

How named routes in Flutter eliminate duplication?

I cannot understand the reason why someone should use named routes, with Navigator.pushNamed(), instead of the normal way with Navigator.push().
The tutorial page states that:
if we need to navigate to the same screen in many parts of our apps,
this can result in code duplication. In these cases, it can be handy
to define a “named route,” and use the named route for Navigation
Duplication
How will the duplication be generated when using simple routing and how it will can be eliminated with the use of named routes?
I fail to understand what is the difference of
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
from
Navigator.pushNamed(context, '/second');
in the context of duplication.
Consider you go with Navigator.push() in many widgets:
// inside widget A:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
// inside widget B:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
// inside widget C:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
Now let say you need to change your App and the widget SecondRoute needs to receive a value on it's constructor. Now you have a problem, since you have multiple copies of the same code on several locations, you need to make sure you will update all of those copies, which can be tedious and error prone:
// inside widget A:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute(
title: 'Title A',
)),
);
// inside widget B:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute(
title: 'Title B',
)),
)),
);
// inside widget C:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute(
title: 'Title A', // ERROR! Forgot to change the variable after a copy/paste
)),
)),
);
Now let's consider you go with named routes.
Firstly I would never recommend anyone to actually use the name directly for navigation, but instead use a static variable reference, this way if you need to change it in the future its way simpler and secure, as you can't forget to update it anywhere, like this:
class Routes {
static const String second = '/second';
}
Another way is to have a reference inside the route itself, a static const String inside SecondRoute, so we can use it as SecondRoute.routeName. It's a matter of personal preference IMO.
Then your widgets will navigate using:
// inside widget A:
Navigator.pushNamed(context, Routes.second); // Routes.second is the same as '/second'
// inside widget B:
Navigator.pushNamed(context, Routes.second);
// inside widget C:
Navigator.pushNamed(context, Routes.second);
Now if you need to pass a parameter to SecondRoute upon creation you can do it in a centralized location using the MaterialApp onGenerateRoute, as this tutorial explains in more detail. Your code will be changed to:
// inside widget A:
Navigator.pushNamed(context, Routes.second, arguments: 'Title A');
// inside widget B:
Navigator.pushNamed(context, Routes.second, arguments: 'Title B');
// inside widget C:
// You can still make mistakes here, but the chances are smaller.
Navigator.pushNamed(context, Routes.second, arguments: 'Title C');
MaterialApp(
onGenerateRoute: (settings) {
if (settings.name == Routes.second) {
final String title = settings.arguments;
return MaterialPageRoute(
builder: (context) => SecondRoute(title: title),
);
}
},
);
The amount of duplicated code is decreased, but on the other hand the onGenerateRoute code gets more complex as you make more routes, as all of their creation will be centralized there, so IMHO it's more about a personal preference then a general guideline.
Push and PushNamed have the similar effect, Push will switch to the route you specified while PushNamed will switch to the route with the route name specified.
What the Tutorial page means for duplication is duplication of code not duplication of routes.
For instance, you have a route where you would want to check whether the user is signed in and show the corresponding page
Using Push only:
Page1:
//This is page 1....
RaisedButton(
child: Text('Go to second'),
onPressed: () {
if (user.state = "login") {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => SecondPage(),
),
)
}else{
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => SecondPageAnonymous(),
),
)
}
}
)
....
In another page, Page2, you will need to repeat the same code:
//This is page 2....
RaisedButton(
child: Text('Go to second'),
onPressed: () {
if (user.state = "login") {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => SecondPage(),
),
)
}else{
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => SecondPageAnonymous(),
),
)
}
}
)
....
With PushNamed, you just have to declare it once and you can basically reuse it over and over again.
In your onGenerateRoute:
onGenerateRoute: (settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => FirstPage());
case '/second':
if (user.state = "login") {
return MaterialPageRoute(
builder: (_) => SecondPage()
);
}else{
return MaterialPageRoute(
builder: (_) => SecondPageAnonymous()
);
}
default:
return _errorRoute();
}
},
Now in ANY pages in your project, you could do this:
Navigator.of(context).pushNamed('/second')
Without needing to repeat the checking of sign in or even the error handling every time you used it. The obvious benefit is that you can stay consistent throughout the app by preventing duplicate code piece, instead of repeating it again and again.
Now, this however DOES NOT prevent duplicates of routes! There is no different between push and pushNamed in this context!
But since your routes are now named, you can do popUntil('/') easily to go back to the first instance of the route, instead of creating it again or PushReplacementNamed.
The only advantage I can see using Navigate with named routes is to have routes declared inside your MaterialApp, so that developer can only be used assigned routes i.e widgets, pages,
If anyone uses other than that, It will give an error 'onUnknownRoute is called.'
Here is my beginner flutter thoughts:
It makes the code cleaner: Without declaring the routes at the level higher widgets, new screens will appear out of nowhere, in response to anything that happens in the app. It is much easier to understand the navigation skeleton/ structure when you declare the routes together, and even more so at a higher widget, especially for other developers. Of course, this doesn't help with understanding exactly when those routes are actually navigated to, but its a small improvement, and brings us back into the declarative paradigm. The hint provided by the declared routes will help a newer developer understand your navigation flow.
For folks visiting this question in 2022. Flutter actually now recommends not using named routes
Note: Named routes are no longer recommended for most applications. For more information, see Limitations in the navigation overview page.
https://docs.flutter.dev/cookbook/navigation/named-routes
If you use push(), you have to import the file in which SecondRoute is located every time you need to navigate to that screen. This is excessive code duplication for big projects that you need to navigate around the different screens.
If you use pushNamed(), you need to define the routes only once in your MaterialApp. Then, you can navigate to any screen from anywhere without repeating the same thing like you have to with push().
Another big reason to choose PushNamed() over the other one is to be able to build your own navigation system with it. You can decide whether or not routes are available for certain users even before they navigate to the screen.
for understanding why we should use Navigator.pushNamed instead Navigator.push first let's be familiar with Navigator methods. did you ever heart about Navigator.popUntil or Navigator.pushAndRemoveUntil?
we use Navigator.popUntil when we want to pop in the stack to a specific route. if you check the documentation you can find that it's very easy to use these methods with the pushNamed method. also, check all methods in the documentation. when I try to understand routing in flutter this article was very useful for me.
and as a disadvantage, it's very hard to handle parameters in this approach. you should create onGenerateRoute and handle parameters for each route.

Present modally in Flutter?

How to do a modal presentation of a route in Flutter?
I figured out how to navigate to a route using the usual "push" transition, but I am struggling to implement a modal transition. See animation attached (done using native iOS). How do I present a screen modally (a screen that itself can be pushed more screens to).
See an example below. The transition I am struggling with is from "A" to "C" (and of course a way to dismiss it and go back to "A").
You can push like this:
Navigator.of(context).push(
CupertinoPageRoute(
fullscreenDialog: true,
builder: (context) => SomePage(),
),
);
Hope this helps.
Go to Section B(push) animation can be achieved by following:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SectionBRoute()),
);
Go to Section C(present modally) animation can be achieved by following:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SectionCRoute(), fullscreenDialog: true),
);

Flutter: Move to a new screen without providing navigate back to previous screen

I'm implementing an authentication flow in my Flutter app.
After a sign in attempt, the CheckAuth (which checks whether a user is signed in or not and then opens home screen or sign up screen accordingly) is opened with this code:
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");
}
});
}
Problem: I can successfully sign in to the app, but if I tap back button after I sign in, it goes back to the sign in screen (while I expect it to exit from the app).
Question: How to move from one screen to another in Flutter without the way back?
Do I need to somehow delete the navigator history? Or don't use navigator at all? I tried Navigator.replace method, but it didn't seem to work.
You need to use Navigator.pushReplacement when leaving the auth screen too. Not just when redirecting to login page.
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,
);
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.
I think you probably have already solved this. But you can set "automaticallyLeadingImplied: false" in the AppBar of the Scaffold you are navigating to.
Just simply add the below code:
Navigator.of(context).pushNamedAndRemoveUntil('/routeName', (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 have resolved this by popping from current page and showing new page:
Navigator.pop(context);
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => newPage));
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
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, '/');
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.
if you are working with Getx state managemaent then you can try this
Get.of(()=> NewPage());