pushReplacement<T extends Object?> method

Future<T?> pushReplacement<T extends Object?>(
  1. Widget sheet, {
  2. SideSheetPosition? position,
  3. SideSheetTransitionBuilder? transitionBuilder,
  4. DecorationBuilder? decorationBuilder,
  5. Duration? animationDuration,
  6. Duration? reverseAnimationDuration,
})

Replace the current sheet of the navigation stack by pushing the given sheet and then disposing the previous sheet once the new sheet has finished animating in.

Implementation

Future<T?> pushReplacement<T extends Object?>(
  Widget sheet, {
  SideSheetPosition? position,
  SideSheetTransitionBuilder? transitionBuilder,
  DecorationBuilder? decorationBuilder,
  Duration? animationDuration,
  Duration? reverseAnimationDuration,
}) async {
  if (!mounted || _blockGestures) return null;

  if (_sheetEntries.isEmpty) {
    throw StateError('No active sheets to replace.');
  }

  final oldEntry = _sheetEntries.last;
  final oldCompleter = oldEntry.completer as Completer<T?>;

  final newEntry = SideSheetEntry<T?>.createNewElement(
    tickerProvider: this,
    index: oldEntry.index,
    decorationBuilder: decorationBuilder ?? oldEntry.decorationBuilder,
    sheet: sheet,
    completer: oldCompleter,
    transitionBuilder: transitionBuilder ?? oldEntry.transitionBuilder,
    position: position ?? oldEntry.position,
    dismissible: oldEntry.dismissible,
    animationDuration: _setSettleDuration(animationDuration),
    reverseDuration: _setReverseSettleDuration(reverseAnimationDuration),
  );

  if (mounted) {
    _sheetEntries.add(newEntry);
    _notifyStateChange();
  }

  // Wait for the end of the sheet navigation animation.
  await Future.delayed(_scrimAnimationController.duration!);
  await Future.delayed(const Duration(milliseconds: 17));

  // Now remove the old sheet entry from the navigation stack.
  if (mounted) {
    _removeSheetSilently(oldEntry);
    _notifyStateChange();
  }

  return oldCompleter.future;
}