showTopSnackBar function

void showTopSnackBar(
  1. BuildContext context,
  2. Widget child, {
  3. Duration showOutAnimationDuration = const Duration(milliseconds: 200),
  4. Duration hideOutAnimationDuration = const Duration(milliseconds: 550),
  5. Duration displayDuration = const Duration(milliseconds: 3000),
  6. double additionalTopPadding = 16.0,
  7. VoidCallback? onTap,
  8. OverlayState? overlayState,
})

Shows a SnackBar at the top of the screen.

Implementation

void showTopSnackBar(
  BuildContext context,
  Widget child, {
  Duration showOutAnimationDuration = const Duration(milliseconds: 200),
  Duration hideOutAnimationDuration = const Duration(milliseconds: 550),
  Duration displayDuration = const Duration(milliseconds: 3000),
  double additionalTopPadding = 16.0,
  VoidCallback? onTap,
  OverlayState? overlayState,
}) async {
  overlayState ??= Overlay.of(context);
  late OverlayEntry overlayEntry;
  overlayEntry = OverlayEntry(
    builder: (context) {
      return _TopSnackBar(
        showOutAnimationDuration: showOutAnimationDuration,
        hideOutAnimationDuration: hideOutAnimationDuration,
        displayDuration: displayDuration,
        additionalTopPadding: additionalTopPadding,
        onTap: () => onTap?.call(),
        onDismissed: () {
          overlayEntry.remove();
          _previousEntry = null;
        },
        child: child,
      );
    },
  );

  _previousEntry?.remove();
  overlayState.insert(overlayEntry);
  _previousEntry = overlayEntry;
}