Is there any way to pop several pages and reload the destination page? - flutter

I'm setting up an app which have to fill a long 4-part form, which might be displayed in 4 sequential screens in a mobile application. Now on the 4th part of the form, there is a submit button. User clicks on Submit and all the previous screens should be popped and user should be taken back to the Item-List screen(1st screen) where the updated data items must be shown. We won’t be pushing anything new here, just going back to a previous route. But the first page should be reloaded. How is this possible?

// Updated Answer
So if this is your first screen where you are pushing and where you want to call a function when poped, this is how you can do it.
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => RegisterScreen()))
.then((success) {
if(success){
// call your function here
}
});

Related

How to update state when Navigating back to a page Flutter

i am creating a social media like app, when user clicks on post ,it redirects to a commentpage,in commentspage data is coming from a seperate API,so when go back using Navigator.pop(context), the data in home is not updated, if pressed like button,its not reflecting in ui, but updating in Api,
i wrapped Navigator with setstate but its not reflecting,
Use this on navigator push Navigator.push(context).then((value)=>setState((){}); when it pop the screen it will run the setState
Use this sentence to go to the second page so the function will wait second screen to close
bool result = await Navigator.push(context);
if(result){setState((){});}
when u close the second screen use this sentence Navigator.pop(context, true);
so when u comment has been posted successfully, u will back to screen one and complete the function if result boolean is true, else do nothing.

How do I make a modal open only the first time a user visits the page?

I am using Bootstrap 5.0.2 as the basis of my website, I finally got the modal to appear on page opening, thanks stack overflow, my aim is to not have the modal appear should the visitor move to another page and then return.
This is the code for running the modal. What do I need to add to make it only function the first time this page is viewed per session?
<script>
window.addEventListener('DOMContentLoaded', () => {
const modal = new bootstrap.Modal(document.querySelector('#myModal'));
modal.show();
});
You would need to implement sessionStorage.
When a user visits the page, we check to see if the sessionStorage contains a value for the modal. If it is the first time they load the page, it would not contain a value as we haven't set anything to it yet. In that case, we show the modal as normal and set a value for the sessionStorage. That way, when the user refreshes the page the value is already set in sessionStorage so the if statement will not run.
window.addEventListener('DOMContentLoaded', () => {
if (!sessionStorage.getItem("modal")) {
const modal = new bootstrap.Modal(document.querySelector('#myModal'));
modal.show();
sessionStorage.setItem("modal", "blah");
}
});

Flutter Navigator not working as expected

So I want a user to be able to go back to the '/' named route after he/she reaches upto a certain page. The condition is that when that page is reached, user should be routed to '/' only on Navigator.pop(context).
ie., Lets say I have routes configured such that :
User is at '/' > Clicks a button and pushNamed to '/abc' > Clicks a button and pushNamed to '/xyz' > Clicks a button and pushNamed to '/mnq'
Now, I want that when user is at /mnq and Navigator.pop(context) is executed, user should be sent back to '/' and not anywhere else.
When I'm at /xyz, I tried to do a Navigator.pushNamedAndRemoveUntil(context, '/mnq', ModalRoute.withName('/'), arguments:....) but It does not work because when Navigator.pop is executed after reaching /mnq, I see a black screen.
Is there a way to do this. Please note that I need to send arguments from /xyz to /mnq.
You can try use pushReplacementNamed()
'/' pushNamed to '/abc' pushReplacementNamed to '/xyz' pushReplacementNamed to '/mnq'
read this article for more information
pushNamedAndRemoveUntil is remove all page before. when you use back/pop, it will show black because no page before. you can pass argument with pushReplacementNamed Flutter - Pass values between routes using pushReplacementNamed or store data in local https://pub.dev/packages/shared_preferences
1st. on pressing a button to user should be sent back to '/'
simply use Navigator.popUntil(context, ModalRoute.withName('/'));
in place of Navigator.pop(context);
2nd. if user tab back button
WillPopScope(
onWillPop: () async {
Navigator.popUntil(context, ModalRoute.withName('/'));
return false;
},
child: Scaffold(......),
)

Navigating to the updated page in flutter

When I insert data from other page and pop this page to go back on the listing page, I have to refresh the listing page to get the updated list of data. I want to go back to the updated version of page without pressing refresh button. I am using pushNamed and also I want to have a back arrow button on the input page.
Using Navigator.push or Navigator.pushNamed returns a Future object to you.
This Future resolves when you use Navigator.pop in the your second screen.
Here is a basic example. Assuming you have function goToNewPage which you are calling to push a new Screen, you can do it like this.
class Screen1State extends State<Screen1> {
void goToNewPage (context) {
Navigator.of(context).pushNamed('screen2').then((_) {
// The screen2 has popped. so you can just call setState here to just rebuild
// You can do anything you want here, that needs to happen after the new page has been popped.
setState(() {});
});
}
... rest of your code.
Note, the actual implementation of what you should do inside the callback depends on how your widget is getting data. But you can do anything you want to do after your new page is popped inside that callback.

Ionic 4 Navigate to page with new data

I have a home page that displays results based on some user information. From results page user can navigate to profile page and edit information. After edit information, they navigate back to home page. The problem is, the home page not refreshed with new results. It still shows old results. How can i reload this page with the new data.
home.page.ts:
openProfilePage() {
this.router.navigate(['/profile']);
}
profile.page.ts
onSubmit() {
this.router.navigate(['/home'], {replaceUrl: true});
}
You can trigger a call you need to make to update the info on:
ionViewDidEnter() {
console.log('ionViewDidEnter ');
// call update function
}
ionViewWillEnter(){
//here you check data is changed or not and call your function according to that this method call every time when navigate to home page
}
The main interface for my app is dynamically built based upon whether or not the app has been activated. I created a previous route service that uses Angular’s Activated Route to detect if the main interface needs to be updated without doing a complete refresh.