navigateOnRightBranch function

bool navigateOnRightBranch(
  1. dynamic context,
  2. ShellBranch targetBranch,
  3. void navigationCallback(
    1. dynamic
    ), {
  4. bool initialLocation = true,
})

Make sure we are on the right navigation branch this needs to be run before you go or push the route or we only switch in the UI and no really in the active shell

Implementation

bool navigateOnRightBranch(
  BuildContext context,
  ShellBranch targetBranch,
  void Function(BuildContext) navigationCallback, {
  bool initialLocation = true,
}) {
  final navState =
      (appShellKey.currentContext?.widget as AppShell).navigationShell;
  if (navState.currentIndex != targetBranch.index) {
    // when routed to chat, we always want to jump to the chat
    // tab
    navState.goBranch(targetBranch.index, initialLocation: initialLocation);
    WidgetsBinding.instance.addPostFrameCallback((Duration duration) {
      // We need the UI branch to actually switch first
      // and on first switching to it, it might even need to create the
      // BuildContext, thus we can’t optimized based on that either :(
      navigationCallback(targetBranch.key.currentContext ?? context);
    });
    return true;
  }
  navigationCallback(context);
  return false;
}