Flutter GetX complex Navigation

Viewed 82

EDIT:

The problem is regarding navigation, Im using GetX for navigation, and the problem is that using Get.back() isn't refreshing the state of the page that I want to get back to, so I need to use Get.toNamed.

This onPressed is used on my back button (on "screen2")

I can go to "screen2" page from "home", "screen1" and "screen3".

"home" and"screen1" are parent screens to "screen2" (is you can say it like that), but "screen3" is only accessed from "screen2".

Now, when I return to "screen2" from "screen3", GetX saves the previous route of "screen3"

and when I press the back button(on "screen2") , it should either go to "home" or "screen1", depends on from where I opened "screen2"

It's pretty simple, but it's hard to explain in a simple way

enter image description here

EDIT: The upper part was easy, but now, there is the final boss: I believe that those below are all the routes.

The problem is probably around about path screen, cause it is the part where the most data goes through.

Home -> about mountain,
Home -> about path,
Home -> about poi,
Home -> about mountain -> about path 
Home -> about mountain -> about path -> about poi
Home -> mountains screen -> about mountain -> about path
Home -> mountains screen -> about mountain -> about path -> about poi
Home -> paths screen -> about path
Home -> paths screen -> about path -> about poi

All possible routes are listed above enter image description here

"screen2" is the same as "ABOUT MOUNTAIN"

I don't know if there is a better way of doing this other that using a bunch of conditionals.

enter image description here (this is clearly wrong, there must be a more elegant approach)

4 Answers

define help var in onpressed function.try this:

    onPressed: () {
final String help='';
                          if (Get.previousRoute == AppRoutes.home) {
                            help = Get.previousRoute.toString();
                            Get.toNamed(AppRoutes.home);
                          } else if(Get.previousRoute == AppRoutes.screen2){
                            String help = Get.previousRoute.toString();
                            Get.toNamed(AppRoutes.screen2);
                          } else{
                            Get.toNamed(help);  //can't use variable help here
                          }
                        },

Try adding argument returnUrl which contains home or Screen1 to your url to screen2 and pass the same argument to Screen3.

Get the argument returnUrl using

Get.arguments['returnUrl'];

Use the argument value to route from screen2 to home/screen1

I have read your comments and found that you need a previous route on another page my suggestion for you is that just store the previous route in local storage using the shared_preferences package and call that route on a particular variable once the page pop out then delete that route from storag.

https://pub.dev/packages/shared_preferences

Function (in the controller) for accurate back navigation (back button) on the about path screen (function also for default android back button) the commented numbers indicate which entire "navigation path" refers to (see the image for better understanding) via [arguments] information about the current screen is sent to the next screen, and the current screen receives information about the previous screen via [Get.arguments]. longer paths will send a list (or an array) of all previous screens as an argument

11: home_screen --> about_path --> home_screen

9: home_screen --> paths --> about_path --> ... --> home_screen

10: home_screen --> paths --> about_path --> about_poi --> about_path --> ... -> home_screen

2: home_screen --> about_mountain --> about_path --> ... --> home_screen

12: home_screen --> about_path --> about_poi --> about_path --> ... --> home_screen

6: home_screen --> mountains --> about_mountain --> about_path --> ... --> home_screen

3: home_screen --> about_mountain --> about_path --> about_poi --> about_path --> ... --> home_screen

7: home_screen --> mountains --> about_mountain --> about_path --> about_poi --> about_path --> ... --> home_screen

will have to change it to switch in the future

void routingAboutPath() {
    if (Get.previousRoute == AppRoutes.home) {
      Get.toNamed(AppRoutes.home); // 11
    } else if (Get.previousRoute == AppRoutes.paths) {
      Get.toNamed(AppRoutes.paths); // 9
    } else {
      if (Get.arguments[0] == AppRoutes.paths) {
        Get.toNamed(AppRoutes.paths); // 10
      } else if (Get.arguments[0] == AppRoutes.home) {
        if (Get.arguments[1] == AppRoutes.mountains) {
          Get.toNamed('${AppRoutes.mountains}/${Get.arguments[2]}', arguments: Get.arguments); // 2
        } else {
          Get.toNamed(AppRoutes.home); // 12
        }
      } else if (Get.arguments[0] == AppRoutes.mountains) {
        Get.toNamed('${AppRoutes.mountains}/${Get.arguments[2]}', arguments: Get.arguments); // 6
      } else if (Get.arguments[0][1] == AppRoutes.mountains) {
        Get.toNamed('${AppRoutes.mountains}/${Get.arguments[0][2]}', arguments: Get.arguments); // 3 i 7
      }
    }
  }

enter image description here

This is only for the about path page, but it works the same for other screens. This one was the most challenging cause the most traffic is going trough that page.

enter image description here Here you can see some navigation lists that hold the "history of navigation" that is used to navigate back to the correct previous screen

Related