showSnackBar method Null safety

  1. @Deprecated('Use ScaffoldMessenger.showSnackBar. ' 'This feature was deprecated after v1.23.0-14.0.pre.')
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showSnackBar (
  1. SnackBar snackbar
)
@Deprecated('Use ScaffoldMessenger.showSnackBar. ' 'This feature was deprecated after v1.23.0-14.0.pre.')

ScaffoldMessengerState.showSnackBar shows a SnackBar at the bottom of the scaffold. This method should not be used, and will be deprecated in the near future..

A scaffold can show at most one snack bar at a time. If this function is called while another snack bar is already visible, the given snack bar will be added to a queue and displayed after the earlier snack bars have closed.

To control how long a SnackBar remains visible, use SnackBar.duration.

To remove the SnackBar with an exit animation, use ScaffoldMessengerState.hideCurrentSnackBar or call ScaffoldFeatureController.close on the returned ScaffoldFeatureController. To remove a SnackBar suddenly (without an animation), use ScaffoldMessengerState.removeCurrentSnackBar.

See ScaffoldMessenger.of for information about how to obtain the ScaffoldMessengerState.

Here is an example of showing a SnackBar when the user presses a button.
To create a local project with this code sample, run:
flutter create --sample=material.ScaffoldState.showSnackBar.1 mysample

See also:

Implementation

@Deprecated(
  'Use ScaffoldMessenger.showSnackBar. '
  'This feature was deprecated after v1.23.0-14.0.pre.'
)
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showSnackBar(SnackBar snackbar) {
  _snackBarController ??= SnackBar.createAnimationController(vsync: this)
    ..addStatusListener(_handleSnackBarStatusChange);
  if (_snackBars.isEmpty) {
    assert(_snackBarController!.isDismissed);
    _snackBarController!.forward();
  }
  late ScaffoldFeatureController<SnackBar, SnackBarClosedReason> controller;
  controller = ScaffoldFeatureController<SnackBar, SnackBarClosedReason>._(
    // We provide a fallback key so that if back-to-back snackbars happen to
    // match in structure, material ink splashes and highlights don't survive
    // from one to the next.
    snackbar.withAnimation(_snackBarController!, fallbackKey: UniqueKey()),
    Completer<SnackBarClosedReason>(),
    () {
      assert(_snackBars.first == controller);
      hideCurrentSnackBar(reason: SnackBarClosedReason.hide);
    },
    null, // SnackBar doesn't use a builder function so setState() wouldn't rebuild it
  );
  setState(() {
    _snackBars.addLast(controller);
  });
  return controller;
}