Flutter show confirmation pop up before exiting from app - flutter

I want to take confirmation from user before leaving the app, ideally when clicked on default system back navigation button and at time my navigation stack is empty.
Suppose I am on my homepage after login, and when I try to go back instead of returning me to login page it should show a pop up dialog. And based on the selection either close the app or leave it open.

wrap your home screen widget with WillPopScope widget and inside onWillPop callback write show your dialog and take action based on user's choice

Related

Flutter navigation pop from which screen

Is there anyway to check route name of last screen after pop? When application start and land on home screen, there are several widgets like view profile, product carousel and so on.
Scenario: User navigate into product listing page, then detail page, click purchase and perform actions. After user purchased, shows purchased successful screen, call Navigator.of(context).popUntil(routeName) back to home screen.
What I want to achieve: After land in home screen, programmatically call api to refresh my balance. Route Observer able to detect navigation back to home screen with didPopNext() method. But this is called no matter it pop from which screen. Therefore the api will repeatedly called which is not ideal. How do I know it was pop from purchased successful screen instead of product listing screen?
Grateful on any helps and hints!
If you where using pop() then you have an option to return some data to previous route but since you are using popUntil() you won't be able to do this. So I think you should clear every route and push home route again from purchased successful page using either pushAndRemoveUntil() or pushNamedAndRemoveUntil(). That means you can now pass an argument to home screen by which you can decide whether to call the API or not.
Navigator.of(context).pushNamedAndRemoveUntil(
'/',
(Route<dynamic> route) => false,
arguments: []
);
Another option would be to figure out a way to return data using popUntil(), Something like this, maybe?

Show Global SnackBar regardless of page

I am trying to show a SnackBar that appears regardless of where in the app I am. I did set up and GlobalKey on my root Scaffold, and I am able to call it to show a snackbar from anywhere in my code BUT since I am inside other Scaffolds, it does not show on these pages. The only way to view it is to pop the routes and go back to the main screen, where the root Scaffold is.
Is there any way I could show a SnackBar that's above every single widget?
This I am trying to do to be able to show a message for notifications received when the app is in foreground

How to clear the navigation stack in Flutter

I want my application to a have Welcome screen, with registration and login option. When I tap on "registration" button to navigate to my Registration screen, i use:
Navigator.push
When I press the "back" button within the Registration screen, it must return to the Welcome page.
But after the user completes the registration I'm using the following code in the Homepage to navigate to my home.
Navigator.pushReplacement
The problem is, when I pressed the "back" button, my app is coming back to the Welcome screen, instead of staying within the Home.
Any idea what is going on?
You can use the following method to clear the navigation stack when you navigate:
Navigator.of(ctx).pushAndRemoveUntil(YourRoute, (Route<dynamic> route) => false);
The second parameter is a Predicate to decide weather the route will be deleted from the stack or not.
Using this method, you'll clear the whole stack before navigating to the route, that way, you won't be able to go back to your Welcome page.

Change state without refreshing

I have a home icon in the navbar whenever the user will click on it, It will take the user to the Home Page. In the Home Page, there are few form fields that I don't want to change when coming back to the Home Page from the navbar Home icon. The following is the code that I am using:
back_to_home(){
this.navCtrl.push( HomePage )
}
By using the above, state is refreshed and changes. Is there way to change the state without refreshing the state?

User get confirm dialogue box when user navigate from the current Page in GWTP

User need to get the confirm dialogue or want to send some backend message like am leaving from the page in GWTP.
In GWT-P there is no specific widget or event handler available for to check the widget is going out of focus from the user screen.
Is there any default widget or event handler for to check the widget or view page get out of focus means user navigating current page to another page.
Your help much appreciated!!