In a Nested Navigator Structure of Flutter, How do you get the a Specific Navigator?

前端 未结 2 1972
孤城傲影
孤城傲影 2020-12-28 15:06

I have this problem when I have Nested Navigators. So something like,

class App extends StatelessWidget {
  @override
  Widget build(BuildContex         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 15:34

    Actually you'd have to use nested Navigator when you have a sub navigation flow or inner journey. Please read the docs of nesting navigators.

    However to get access to root navigator, you can recursively look for the parent Navigator from current Navigator and return current Navigator when it has no parent Navigator.

    Example:

    NavigatorState getRootNavigator(BuildContext context) {
      final NavigatorState state = Navigator.of(context);
      try {
        return getRootNavigator(state.context);
      } catch (e) {
        return state;
      }
    }
    
    //use it from any widget like
    getRootNavigator(context);
    

    EDIT:

    Solution 1 :

    To get a specific parent Navigator, I can think of extending current Navigator class to accept an id and find the Navigator by id. Something like:

    class NavigatorWithId extends Navigator {
      const NavigatorWithId(
          {Key key,
          @required this.id,
          String initialRoute,
          @required RouteFactory onGenerateRoute,
          RouteFactory onUnknownRoute,
          List observers = const []})
          : assert(onGenerateRoute != null),
            assert(id != null),
            super(
              key: key,
              initialRoute: initialRoute,
              onGenerateRoute: onGenerateRoute,
              onUnknownRoute: onUnknownRoute,
              observers: observers,
            );
    
      // when id is null, the `of` function returns top most navigator
      final int id;
    
      static NavigatorState of(BuildContext context, {int id, ValueKey key}) {
        final NavigatorState state = Navigator.of(
          context,
          rootNavigator: id == null,
        );
        if (state.widget is NavigatorWithId) {
          // ignore: avoid_as
          if ((state.widget as NavigatorWithId).id == id) {
            return state;
          } else {
            return of(state.context, id: id);
          }
        }
    
        return state;
      }
    }
    

    Use NavigatorWithId instead of Navigator whenever required, like

    return NavigatorWithId(
      id: 1,
      initialRoute: '/',
      onGenerateRoute: (_) =>
          MaterialPageRoute(builder: (_) => const YourPage()),
    )
    

    Then access it like:

    NavigatorWithId.of(context, id: 1)
    

    Solution 2 :

    Pass ValueKey to the navigator and make a util function that would match key and return the required Navigator.

    A function something like

    NavigatorState getNavigator(BuildContext context, {bool rootNavigator = false, ValueKey key}) {
      assert(rootNavigator != null);
      final NavigatorState state = Navigator.of(
        context,
        rootNavigator: rootNavigator,
      );
      if (rootNavigator) {
        return state;
      } else if (state.widget.key == key) {
        return state;
      }
      try {
        return getNavigator(state.context, key: key);
      } catch (e) {
        return state;
      }
    }
    

    Use

    return Navigator(
      key: const ValueKey('Navigator1'),
      initialRoute: '/',
      onGenerateRoute: (_) =>
          MaterialPageRoute(builder: (_) => const RootPage()),
    );
    

    and access it like

    getNavigator(context, key: const ValueKey('Navigator1'))
    

    The drawback of this method I can see as not all types of keys would be supported.

    Note: I don't claim any of the above solutions to be best or optimal. These are few methods I came up with. If someone can come up with better approach, I'm eager to learn :)

    Hope this helps!

提交回复
热议问题