New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Navigation Observer not being called in tabs router #450
Comments
@rugemo Because tabsRotuer does not use a navigator, the route it self does not have the same lifecycle as stacked routes, tab routes are pushed at the same time and only put in focus depending on the active index, if your tab is a nested navigator you could observe it. |
@Milad-Akarie - Thanks for the explanation, I didn't realise the distinction between stacked routes and tab routes. I'll look into using a nested navigator. |
@rugemo If we're to mimic the life cycle of stacked routes inside of TabsRouter I guess we could use didPush when the tab first pushed then use didReplace for when a tab is switched. |
I reckon didReplace for the tabs register in a similar way to the logs we see now. So after this log
We'd see the first tab registered:
then switching tab to Contacts we'd see the same:
Then lets say contacts has a child page contact_details, we'd see
|
@rugemo @mrgnhnt96 on a second thought tab routes don't have an associated Route object, I guess we're forced to extend the native observer with our own like follows abstract class TabsAwareNavigatorObserver extends NavigatorObserver {
void didInitTabRoute(PageRouteInfo route) {}
void didChangeTabRoute(PageRouteInfo route) {}
} it would be simple to add this to an existing observer for example abstract class TabsAwareFirebaseAnalyticsObserver extends FirebaseAnalyticsObserver implements TabsAwareNavigatorObserver {
void didInitTabRoute(PageRouteInfo route) {}
void didChangeTabRoute(PageRouteInfo route) {}
} @mrgnhnt96 suggested that we should include indexes so it would look something like this abstract class TabsAwareNavigatorObserver extends NavigatorObserver {
void didInitTabRoute(PageRouteInfo route, int index) {}
void didChangeTabRoute(PageRouteInfo route, int previousIndex , int index) {}
} |
@rugemo @mrgnhnt96 sounds good? |
are indexes really important though? I mean you have the route name/path how about just including a previous route like the native observer does abstract class TabsAwareNavigatorObserver extends NavigatorObserver {
void didInitTabRoute(PageRouteInfo route, PageRouteInfo? previousRoute) {}
void didChangeTabRoute(PageRouteInfo route, PageRouteInfo previousRoute) {}
} |
What if we created an |
|
It never hurts to keep a feature open, if you make it available, people will use it. just throwing this out there, what if we used |
So technically you could just use |
using a TabRouteData object sounds like a good idea. class MyObserver extends TabsAwareNavigatorObserver{
// implement tab router method
@override
void didChangeTabRoute(TabRouteData route, TabRouteData previousRoute) {
// TODO: implement didChangeTabRoute
}
// implement native navigator observer methods
@override
void didPush(Route route, Route? previousRoute) {
// TODO: implement didPush
}
// implement other methods
} |
oh I can understand the confusion, but it's a NavigatorObserver that's aware of Tabs, FeatureAwareClass convention seems to be used alot in flutter so I'm not really sure e.g this class [RouteAware] is used to make a widget aware of routes |
Yeah, this is perfect. The name is the only thing catching me haha Totally get the FeatureAwareClass, I can just imagine people getting confused that they are using a “Tabs Aware” class to observe “Routes”. Flutter uses the FeatureAwareClass convention, they also have |
Ah, I see your point too, it’s a Tabs Aware, |
so AutoRouteNavigatorObserver makes sense? or just AutoRouterObserver? |
I would go with AutoRouterObserver. It makes it a little more vague, but they both work 👌 |
Sorry to hijack, but how do you go about observing nested routes? I have added an observer like the code above which works on root routes but doesn't seem to work on nested routes.
|
@amritk You'll need to pass an observer for each navigator, as I dug more into this we can't actually use the same observer for different navigators ( restricted by native Navigator) so in the next build navigatorObservers will be a create-method that returns a list of observers so that every child navigator can call it to create a copy of the provided observes. |
Hey @Milad-Akarie would you happen to know the syntax? I was poking around trying to figure it out but I could not. |
@amritk The syntax of what exactly? |
@Milad-Akarie passing the observer into the nested autorouter |
Please share your home page setup |
hey @Milad-Akarie sure thing:
|
@amritk I just wanted to make sure you get the idea of nesting scoped navigators. |
@Milad-Akarie oooo that's where it goes. Thanks a lot and nice work! |
I've been playing with How can I get an event that tab is supposed to change? I need to make a custom implementation changing selected tab. |
@Milad-Akarie, any updates about this? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions |
Hi,
I've been trying to observe routes using a NavigationObserver. It works fine for root level pages, but when setting
usesTabsRouter: true
it seems child elements are not observed. Is there a way to this, or a way to set another observer for the child routes? I've had a good look through the documentation but couldn't see anything obvious to make this happen.Here are some screenshots showing the code. It's a basic set up, with some print statements in the NavigationObserver callbacks.
As you can see there are callbacks to the nav observer (simple print statements) for the splash > launch > login > AuthenticatedNavigation routes. However there aren't any logs for routes within the 'AuthenticatedNavigationRoute'. See the second image below, navigated from favourites tab to contacts tab, but nothing registered in the observer.
Is there something specific we have to do to observer child routes?
Thanks for all your work on the library, and apologies if there is something obvious I am missing!
The text was updated successfully, but these errors were encountered: