setTabIndex method

void setTabIndex(
  1. int index, {
  2. bool? preserveState,
})

preserveState behaviour: true: subroutes within each tab are preserved false: resets each tab to the first route. null (default): preserves state when switching tabs; resets to the first route when activating the same tab again.

Implementation

void setTabIndex(int index, {bool? preserveState}) {
  final ShellValue updatedValue;

  if (preserveState == null || preserveState == true) {
    if (index != value.tabIndex) {
      updatedValue = value.withIndex(index);
    } else {
      final next = value.tabNodes[index];
      updatedValue = value.withNext(
        next.route.updateWithNext(
          next: null,
          value: next.value,
          popCompleter: next.popCompleter,
        ),
      );
    }
  } else {
    final next = value.tabNodes[index];
    updatedValue = value.withNext(
      next.route.updateWithNext(
        next: null,
        value: next.value,
        popCompleter: next.popCompleter,
      ),
    );
  }

  controller.setStack(
    controller.stack.withUpdatedValue(
      value.key,
      updatedValue,
    ),
  );
}