How to switch the screen smoothly? - flutter

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.

Related

How to set animation in pushReplacementNamed

Two screen movements are being done through pushReplacementNamed.
Navigator.pushReplacementNamed(context, '/screen_b');
Navigator.pushReplacementNamed(context, '/screen_a');
What I want is that the animation behaves in reverse when the screen to screen_b goes back to screen_a.
Now it keeps moving in only one direction. (From right to left)
I am wondering if there is a way other than push and pop.
use CupertinoPageRoute
Navigator. pushReplacementNamed(
context,
CupertinoPageRoute(
builder: (context) => yourpage(),
));
To achieve that you need to use the following in scree_a:
Navigator.pushNamed(context, '/screen_b');
In screen_b you need just pop if you want the back ainmation:
Navigator.pop(context);
And when you create your route, use CupertinoPageRoute:
CupertinoPageRoute(
builder: (_) => ScreenA/B())

Navigation in Flutter App - does not work as intended

I try to build a flutter app and want to navigate trough the pages.
I build a drawer with a menu with several items to open new pages - that works fine.
onTap: () {
Navigator.of(context).pop();
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) => new Page1(),
),
);
},
By pressing on the Page1 menuitem page1 opens fine.
Inside page1 I have a slidable list with "edit" and "details". I open this with
onTap: () {
Navigator.of(context).pop();
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => new DetailsPage(),
),
);
},
In the details Page I see all the things i wanna see, but when I press the Back button in the appBar i am back at home screen with closed drawer.
I tried to put onPressed: () => Navigator.pop(context); inside the appBar section but this doesn´t work either.
Now I got what I want by using always Navigator.push but I am sure that this is not right, so that navigator stack gets bigger and bigger.
What I do wrong? Why .pop does not bring me to the last page?
Any ideas?
Thx in advance
Patrick
Just remove the line with the Navigator.of(context).pop();in the Page1 onTap.
you can use pushReplacement in any cases you need to remove current page from stack and route to new page.
Replace the current route of the navigator that most tightly encloses
the given context by pushing the given route and then disposing the
previous route once the new route has finished animating in.
here is the documentation

How to call a function to update a value after popping a screen in Flutter?

Screen 1: shows list of item with add button.
Screen 2: form to add a new item to the list.
Screen 2 >> Screen 1 - While calling navigator.pop() in screen 2, how to call method/setState (to update list) in screen 1?
Can anyone help me out?
I don't want to relaunch screen again. I just need to run a method to update my list after popping previous screen?
When you navigate from Screen 1 to Screen 2, you use the push method. The push method returns a Future object.
You can use the then method of Future to execute your code after Screen 2 was popped from the navigation stack.
Navigator.of(context)
.push(MaterialPageRoute(
builder: (context) => Screen2(),
))
.then((value) {
// you can do what you need here
// setState etc.
});
You can use Provider or BLoc or you can await for the result when you push the page
You can do it like this :
// this method waits for the pop to be called
var data = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginScreen()),
);
debugPrint(data);
// here the second argument is the data
Navigator.pop(context, "data"); // popped from LoginScreen().
output
data
Same method can also be done like below
// this method waits for the pop to be called
Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginScreen()),
).then((data){
// then will return value when the loginScreen's pop is called.
debugPrint(data);
});
Here is a good article to look into this
https://medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31
Navigator.pop has a second argument called result.
You could then return the value that you need to the first page that will read it as the return value of Navigator.push

Flutter back-pressed shows black screen

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);

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),
);