Navigator pushReplacementNamed loads screen twice in Flutter app - flutter

Inside Flutter app I have MaterialApp with list of named routes and bottom navigation bar with few tabs. On one of tabs I have button with this code on tap
Navigator.of(context).pushReplacementNamed(MyRouteName);
For some reason target screen is loaded few times (it can be seen on screen) and it's initState() method is also called at least twice. Why is it happening and what can be done with it?

I've had the same problem too, I solved it by going back with Navigator.of (context).pop Actually, the solution may vary depending on your status as the route. I suggest you try alternative methods.

Related

Flutter - How to preserve widgets with data and states, when navigating using the NavigationBar class

I'm using the NavigationBar class for the navigation through my app, but I have a problem, which is that I can't preserve the information on the screen when I'm navigating, so everything loads up again at the start of the screen. How should I do to preserve the screen even if I am in another screen so everything doesnt load up again?
You can use bloc for your state management.
Use states. There are a lot of options available like for example stateful widgets.

how to prevent PageView.builder building page I didnt enter yet or delete getxcontroller after I leave the page - flutter/GetXcontroller

sorry in advance if i didnt describe my question clearly:
I have several pages made by PageView.builder, while swiping these pages on main screen left and right, I found their initState is already working, and GetXController inside are also loading their data. I use Get.delete<Controller> while clicking back button and coming back to main screen to remove these data.
However, the state and data will bring back to main screen if I didnt activate Get.delete<Controller>, and therefore next time I come to this page, the state of page will be not default, for instance the button still keep pressed status.
So my question is how to prevent page.builder to build the widget before I come inside to this page, or how to delete getxcontroller if I am not in this page. Thanks a lot!
Try resetting the GetX by calling
Get.reset(); --> this will re-initialize controllers
Alternatively calling
Get.put(ControllerName()); in a build method will re-initialize the controller

How do you create a side navigation drawer that persists across pages?

I've looked through many tutorials for the side nav drawer. I can create one that works fine to lead to different pages. However, when I travel to a page that's different from home, it only gives me the arrow icon to go back to home at the top left instead of keeping the button to bring me back to the side navbar. How can I prevent this?
I can't use the home page to navigate everywhere because it's just supposed to be a blank splash screen.
You can define your drawer in a separate widget file, that you can import everywhere you have a scafold.
I created a package for it because I was missing similar functionality. If you want a Flutter approach for this navigation check out: https://api.flutter.dev/flutter/material/NavigationRail-class.html
Or if you want to have a look at my package: https://pub.dev/packages/side_navigation
It's because you're moving to a new page/Scaffold (probably using Navigator.push()). So, the default button in the AppBar will be the back button.
You can either have the same Drawer in every Scaffold you navigate to, which is not recommended since you'll just keep pushing routes to the navigation stack.
Or, you can change pages within the Scaffold. Check the interactive examples in BottomNavigationBar and NavigationRail to get an idea of how to do it. Basically instead of calling Navigator.push() when a tile in Drawer is tapped, just update the selected index and call setState().

Here Maps Navigate restarts Navigation on page change

I have a problem with my Here Maps Navigation. I can start the navigation on my flutter app but every time I open another page on the app and come back to the navigation page, it restarts the navigation. It tries to get the current location again, takes a lot of time to do that, and shows the default "invalid" map page. I want the navigation to save it's current state even if I change the view to another page on my app and come back to navigating. Is there a way to do this? Am I missing something here?
I finally found a way to solve it by saving the state of my Flutter pages. I used the PageView widget for the main.dart's scaffold body and AutomaticKeepAliveClientMixin on the pages I wanted to save the states of. You can check this YouTube video for how to implement this: https://www.youtube.com/watch?v=fGe45NikVqE

How to distribute state of BottomNavigationBar across the app in Flutter?

In my app, the navigation bar works fine between the tabs, but when I go inside of each item in my list, I want the navigation bar to be there too.
What I did is that I made it a separate widget, and called in the bottomNavigationBar inside the Scaffold of the pages, but the problem is that I am changing the state of the navigation bar in my main page only, so the state doesn't get distributed across all pages in my app.
How do I make the body of the page change in an app where there is not only one body, but many?
Thanks!
Since there is not code over here in your post and I don't know what you have done so far so I can only advise you to go through this post.
I know you might have already seen this, but still, try to follow and this will really help.
Thanks....!!!!!!
https://medium.com/#lucassaltoncardinali/keeping-state-with-the-bottom-navigation-bar-in-flutter-69e4168878e1