Milad-Akarie / auto_route_library

Flutter route generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AutoTabsRouter keeps routes alive.

hasanmhallak opened this issue · comments

basiclly the title.
this is critical because this behavior will not allow initState() to be called, which means there is no way to call any function in initState() to update state of this route.
isn't supposed to be a flag like automaticKeepAlive in case this edge case?
is there any workaround for this?

@hasanmhallak the flag you're looking for is AutoRoute(..., maintainState : false);
not sure what you usecase is but I'd refresh my state using other methods like AutoRouteAwareStateMixin which allows you to override didChangeTabRoute which is called eveytime you change to that tab

After adding AutoRouteObserver to the root delegate or AutoTabsRouter

   routerDelegate: _rootRouter.delegate(
        navigatorObservers: () => [AutoRouteObserver()],
      ),

observer your routes like this

class TabPageState extends State<TabPage> with AutoRouteAwareStateMixin<TabPage> {        
 // only override if this is a tab page        
   @override        
   void didInitTabRoute(TabPageRoute? previousRoute) {}        
 // only override if this is a tab page        
   @override        
   void didChangeTabRoute(TabPageRoute previousRoute) {}        
 }

or you can add a listener to tabsRouter and do something eveytime it changes to the target index

inside of initState of target TabPage

context.tabsRouter.addListener((){
  if(context.tabsRouter.activeIndex == targetIndex){
     // refresh page
  }
 });