setKeysAndCurrentKey method

void setKeysAndCurrentKey(
  1. List<K> keys,
  2. K? currentKey
)

Sets keys and currentKey in one take to only notify listeners once.

If keys did not change, it has the same effect as setting currentKey.

Tries to preserve the currently selected tab. If this key is gone, selects the first one.

Implementation

void setKeysAndCurrentKey(List<K> keys, K? currentKey) {
  if (const ListEquality().equals(keys, _keys)) {
    this.currentKey = currentKey;
    return;
  }

  final newIndex = currentKey == null ? 0 : max(keys.indexOf(currentKey), 0);

  if (_keys.length == keys.length) {
    _keys = keys;
    _indexedController.index = newIndex;
  } else {
    _indexedController
      ..removeListener(_onControllerChanged)
      ..dispose();

    _indexedController = TabController(
      animationDuration: _indexedController.animationDuration,
      initialIndex: newIndex,
      length: keys.length,
      vsync: _vsync,
    );
    _proxyAnimation.parent = _indexedController.animation;
    _indexedController.addListener(_onControllerChanged);

    _keys = keys;
    notifyListeners();
  }
}