mt-akar / bottom_nav_layout

A quick and powerful Flutter layout with a bottom navbar.
https://pub.dev/packages/bottom_nav_layout
MIT License
34 stars 11 forks source link

Detect last pageStack element #7

Closed mohamoha6200 closed 2 years ago

mohamoha6200 commented 2 years ago

Hello and thank you for this great package , What I'm trying to achieve is to be able to detect the last pageStack element so I can prevent the app from exiting once all screens are popped using android back button , what I did in the willPopScope : onWillPop: () { if (myPageStack.peek() == 0) return Future.value(false); return Future.value(true); }, This has 2 downsides , if somehow the last element isn't the initialPage(==0) a no element error raises and the app becomes no longer usable. If the last element is indeed the initialPage which is 0 then pressing the back button does nothing meaning if there are screens pushed (nested) into that initialPage they won't pop being unable to reach previous screens in that last pageStack. My question is , is there a way to simply find out whether the pageStack is pop-able or not like the (maybePop) function in order to know that the current screen is actually the last so we can anticipate the app from going into background ? Thank you looking forward to your input !

mt-akar commented 2 years ago

I think what you are looking for is this method documented here.

Since the PageStack inherits ListQeue, you can use all ListQueue methods on PageStack instances. Using ListQueue.length, you can check how many pages are left in the stack.

mohamoha6200 commented 2 years ago

Thank you for the quick response , The problem with .length is that it doesnt take into account the nested screens pushed into 1 pageStack , I tried if (myPageStack.length == 1) return Future.value(false); and it works only between bottom tabs (pages) and when it gets to the final screen when popping instead of popping the nested screens (like it would if the above line wasnt there) it just gets stuck on the first page while deeply nested with no way pop those nested screens with back button press. Is there a method that tracks the whole ListQueue elements length while considering the nested navigation and not just the pages length ?

mt-akar commented 2 years ago

Okey. Now you are asking for something else. PageStack instance has no information on the in-page stack state. You can add an additional check for that, meaning right after you check the number of destinations in the PageStack instance, you need to also check how many routes are pushed to the in-page navigation stack, which is again not controlled by the PageStack instance.

How to do that is outside the scope of this package. I think Navigator's canPop method documented here or Route's isFirst method documented here should solve your problem.

mohamoha6200 commented 2 years ago

Hey , so I got the behavior I needed using these conditions : // myNavKey : global nav state of the first page

      `  if (myPageStack.length == 1 || myPageStack.peek() == 0) {
        if (myNavKey.currentState.canPop()) {
          myNavKey.currentState.pop();

          return Future.value(false);
        }
      }
      return Future.value(true);`

In my use case only the first Page had nested pages within but to generalize this feature maybe you can consider adding a bool to each page indicating whether there are nested pages pushed inside it or not , having a smooth and logical way of popping screens and navigating through the stack is crucial + right now it's really not easy to know whether you have reached the end of the queue (pageStack + nested navigation) so you can properly anticipate the app going into background.

Thank you again for the quick responses and this amazing package , peace.

mt-akar commented 2 years ago

I feel like what you are trying to do is not very optimal. If your aim is just to run some code while exiting the app, there are better ways to do it. Some discussion can be found here.

If your aim is to run some code exiting the app with the back button, your approach may be reasonable. However, I doubt that you are trying to do that.

This package provides you with boilerplate code that deals with bottom navigation patterns, bottom bar behavior, and coordination between top-level destinations. Figuring out when and how you are exiting the app is out of the scope of this package, though it might be able to help you figure that out as well.

I am glad that you solved your problem. You can always ask me anything about the package if you feel stuck. ^^