setNestingBranch method

void setNestingBranch(
  1. BuildContext context,
  2. T branch, {
  3. bool inChildNavigator = false,
  4. bool resetBranchStack = false,
  5. Object? branchParam,
})

Sets the active nesting branch of the closest parent crossroad.

Setting inChildNavigator to true finds the closest child navigator (crossroad) of a page that is the closest ancestor of the context.

To reset the stack of the branch to be set, set the parameter resetBranchStack to true. While reset meaning to fill the stack with the initial page.

The branchParam parameter is used to set the parameter in the RoutebornBranchParams.

Implementation

void setNestingBranch(
  BuildContext context,
  T branch, {
  bool inChildNavigator = false,
  bool resetBranchStack = false,
  Object? branchParam,
}) {
  if (inChildNavigator) {
    final parentPageNode = _findAncestorPageNode(context);

    if (parentPageNode == null) {
      throw NavigationStackError(
          'Could not find AppPageNode from the current route.\n'
          'Given context Route is not in the pages stack.');
    }

    if (parentPageNode.crossroad == null) {
      throw NavigationStackError(
          'The given context\'s page does not have nested navigation.\n');
    }

    _rootPageNodesSetter = AppPageNodesStackUtil.updateNestedStack(
      parentPageNode.crossroad!.navigatorKey,
      _rootPageStack,
      (previousCrossroad) => previousCrossroad.copyWith(
        activeBranch: branch,
        resetBranch: resetBranchStack,
        branchParam: branchParam,
      ),
    );
  } else {
    final navState = context.findAncestorStateOfType<NavigatorState>();

    final key = navState!.widget.key;

    if (key == rootNavKey) {
      throw NavigationStackError('Cannot set branch on root navigator');
    } else {
      _rootPageNodesSetter = AppPageNodesStackUtil.updateNestedStack(
        key!,
        _rootPageStack,
        (previousCrossroad) => previousCrossroad.copyWith(
          activeBranch: branch,
          resetBranch: resetBranchStack,
          branchParam: branchParam,
        ),
      );
    }
  }

  notifyListeners();
}