showTopSnackBar function

void showTopSnackBar(
  1. OverlayState overlayState,
  2. Widget child, {
  3. Duration animationDuration = const Duration(milliseconds: 1200),
  4. Duration reverseAnimationDuration = const Duration(milliseconds: 550),
  5. Duration displayDuration = const Duration(milliseconds: 3000),
  6. VoidCallback? onTap,
  7. bool persistent = false,
  8. ControllerCallback? onAnimationControllerInit,
  9. EdgeInsets padding = const EdgeInsets.all(16),
  10. Curve curve = Curves.elasticOut,
  11. Curve reverseCurve = Curves.linearToEaseOut,
  12. SafeAreaValues safeAreaValues = const SafeAreaValues(),
  13. DismissType dismissType = DismissType.onTap,
  14. SnackBarPosition snackBarPosition = SnackBarPosition.top,
  15. List<DismissDirection> dismissDirection = const [DismissDirection.up],
})

The overlayState argument is used to add specific overlay state. If you are sure that there is a overlay state in your BuildContext, You can get it Overlay.of(BuildContext) Displays a widget that will be passed to child parameter above the current contents of the app, with transition animation

The child argument is used to pass widget that you want to show

The animationDuration argument is used to specify duration of enter transition

The reverseAnimationDuration argument is used to specify duration of exit transition

The displayDuration argument is used to specify duration displaying

The onTap callback of _TopSnackBar

The persistent argument is used to make snack bar persistent, so displayDuration will be ignored. Default is false.

The onAnimationControllerInit callback is called on internal AnimationController has been initialized.

The padding argument is used to specify amount of outer padding

curve and reverseCurve arguments are used to specify curves for in and out animations respectively

The safeAreaValues argument is used to specify the arguments of the SafeArea widget that wrap the snackbar.

The dismissType argument specifies which action to trigger to dismiss the snackbar. Defaults to TopSnackBarDismissType.onTap

The snackBarPosition argument specifies the vertical position of the snackbar. Defaults to SnackBarPosition.top

The dismissDirection argument specifies in which direction the snackbar can be dismissed. This argument is only used when dismissType is equal to DismissType.onSwipe. Defaults to [DismissDirection.up]

Implementation

void showTopSnackBar(
  OverlayState overlayState,
  Widget child, {
  Duration animationDuration = const Duration(milliseconds: 1200),
  Duration reverseAnimationDuration = const Duration(milliseconds: 550),
  Duration displayDuration = const Duration(milliseconds: 3000),
  VoidCallback? onTap,
  bool persistent = false,
  ControllerCallback? onAnimationControllerInit,
  EdgeInsets padding = const EdgeInsets.all(16),
  Curve curve = Curves.elasticOut,
  Curve reverseCurve = Curves.linearToEaseOut,
  SafeAreaValues safeAreaValues = const SafeAreaValues(),
  DismissType dismissType = DismissType.onTap,
  SnackBarPosition snackBarPosition = SnackBarPosition.top,
  List<DismissDirection> dismissDirection = const [DismissDirection.up],
}) {
  late OverlayEntry _overlayEntry;
  _overlayEntry = OverlayEntry(
    builder: (_) {
      return _TopSnackBar(
        onDismissed: () {
          _overlayEntry.remove();
          _previousEntry = null;
        },
        animationDuration: animationDuration,
        reverseAnimationDuration: reverseAnimationDuration,
        displayDuration: displayDuration,
        onTap: onTap,
        persistent: persistent,
        onAnimationControllerInit: onAnimationControllerInit,
        padding: padding,
        curve: curve,
        reverseCurve: reverseCurve,
        safeAreaValues: safeAreaValues,
        dismissType: dismissType,
        snackBarPosition: snackBarPosition,
        dismissDirections: dismissDirection,
        child: child,
      );
    },
  );

  if (_previousEntry != null && _previousEntry!.mounted) {
    _previousEntry?.remove();
  }

  overlayState.insert(_overlayEntry);
  _previousEntry = _overlayEntry;
}