expand method

Future<bool> expand(
  1. int index
)

Expands the item at the given index, returning true if succesfully expanded. It collapses the least recently expanded item if the maximum number of expanded items is reached.

This method should typically not be called while the widget tree is being rebuilt.

Implementation

Future<bool> expand(int index) async {
  final controller = _controllers[index];
  if (_expanded.contains(index) || controller == null) {
    return false;
  }

  final futures = <Future<void>>[];
  if (_max != null && _max <= _expanded.length) {
    final collapsing = _expanded.firstOrNull;
    if (collapsing == null) {
      return false;
    }

    _expanded.remove(collapsing);
    futures.add(_controllers[collapsing]!.reverse());
  }

  _expanded.add(index);
  futures.add(controller.forward());

  await Future.wait(futures);
  notifyListeners();

  return true;
}