snackBar method

void snackBar({
  1. Key? key,
  2. Widget? content,
  3. String? message,
  4. Color? backgroundColor,
  5. double? elevation,
  6. EdgeInsetsGeometry? margin,
  7. EdgeInsetsGeometry? padding,
  8. double? width,
  9. ShapeBorder? shape,
  10. SnackBarBehavior? behavior,
  11. SnackBarAction? action,
  12. Duration? duration,
  13. Animation<double>? animation,
  14. VoidCallback? onVisible,
  15. DismissDirection? dismissDirection,
  16. Clip? clipBehavior,
  17. int? durationMillis,
  18. int? animationDurationMillis,
})

Display the SnackBar

Implementation

void snackBar({
  Key? key,
  Widget? content,
  String? message,
  Color? backgroundColor,
  double? elevation,
  EdgeInsetsGeometry? margin,
  EdgeInsetsGeometry? padding,
  double? width,
  ShapeBorder? shape,
  SnackBarBehavior? behavior,
  SnackBarAction? action,
  Duration? duration,
  Animation<double>? animation,
  VoidCallback? onVisible,
  DismissDirection? dismissDirection,
  Clip? clipBehavior,
  int? durationMillis,
  int? animationDurationMillis,
}) {
  //
  final context = this.context;

  if (useMaterial) {
    //
    if (content == null) {
      if (message != null) {
        message = message.trim();
        if (message.isNotEmpty) {
          content = Text(message);
        }
      }
      if (content == null) {
        return; // Nothing to display
      }
    }

    ScaffoldMessengerState? state;

    if (context != null) {
      state = ScaffoldMessenger.maybeOf(context);
    }

    state ??= appState?.scaffoldMessengerKey?.currentState;

    state?.showSnackBar(
      SnackBar(
        key: key,
        content: content,
        backgroundColor: backgroundColor,
        elevation: elevation,
        margin: margin,
        padding: padding,
        width: width,
        shape: shape,
        behavior: behavior,
        action: action,
        duration: duration ?? const Duration(milliseconds: 4000),
        animation: animation,
        onVisible: onVisible,
        dismissDirection: dismissDirection ?? DismissDirection.down,
        clipBehavior: clipBehavior ?? Clip.hardEdge,
      ),
    );
  } else {
    //
    if (context == null) {
      return;
    }

    if (message == null) {
      return; // Nothing to display
    }

    message = message.trim();

    if (message.isEmpty) {
      return; // Nothing to display
    }

    animationDurationMillis ??= 200;
    durationMillis ??= 3000;

    final overlayEntry = OverlayEntry(
      builder: (context) => _CupertinoSnackBar(
        key: key,
        message: message!,
        animationDurationMillis: animationDurationMillis!,
        waitDurationMillis: durationMillis!,
      ),
    );

    Future.delayed(
      Duration(milliseconds: durationMillis + 2 * animationDurationMillis),
      overlayEntry.remove,
    );
    Overlay.of(context).insert(overlayEntry);
  }
}