showSnackBar static method

void showSnackBar(
  1. BuildContext context, {
  2. required Widget content,
  3. bool hideWithAnimation = true,
  4. SnackBarAction? action,
  5. Animation<double>? animation,
  6. Color? backgroundColor,
  7. SnackBarBehavior? behaviour,
  8. Duration duration = const Duration(seconds: 4),
  9. double? elevation,
  10. EdgeInsetsGeometry? margin,
  11. EdgeInsetsGeometry? padding,
  12. ShapeBorder? shape,
  13. double? width,
  14. SnackBarType snackBarType = SnackBarType.normal,
  15. SnackBarShape snackBarShape = SnackBarShape.normal,
})

Memunculkan snackbar

Implementation

static void showSnackBar(
  BuildContext context, {
  required Widget content,
  bool hideWithAnimation = true,
  SnackBarAction? action,
  Animation<double>? animation,
  Color? backgroundColor,
  SnackBarBehavior? behaviour,
  Duration duration = const Duration(seconds: 4),
  double? elevation,

  /// If margin not null , set default behaviour = SnackBarBehaviour.floating
  EdgeInsetsGeometry? margin,
  EdgeInsetsGeometry? padding,
  ShapeBorder? shape,
  double? width,
  SnackBarType snackBarType = SnackBarType.normal,
  SnackBarShape snackBarShape = SnackBarShape.normal,
}) {
  final scaffoldMessager = ScaffoldMessenger.of(context);

  Color? _backgroundColor = backgroundColor;

  if (hideWithAnimation) {
    scaffoldMessager.hideCurrentSnackBar();
  } else {
    scaffoldMessager.removeCurrentSnackBar();
  }

  if (margin != null) {
    // ignore: parameter_assignments
    behaviour = SnackBarBehavior.floating;
  }

  switch (snackBarType) {
    case SnackBarType.success:
      _backgroundColor = Colors.green;
      break;

    case SnackBarType.error:
      _backgroundColor = Colors.red;
      break;

    case SnackBarType.warning:
      _backgroundColor = Colors.orange;
      break;

    case SnackBarType.info:
      _backgroundColor = Colors.lightBlue;
      break;

    default:
      // ignore: parameter_assignments
      _backgroundColor = backgroundColor;
      break;
  }

  switch (snackBarShape) {
    case SnackBarShape.rounded:
      shape = RoundedRectangleBorder(borderRadius: BorderRadius.circular(15));
      break;
    default:
      // ignore: parameter_assignments
      shape = RoundedRectangleBorder(borderRadius: BorderRadius.circular(0));
      break;
  }

  scaffoldMessager.showSnackBar(
    SnackBar(
      content: content,
      action: action,
      animation: animation,
      backgroundColor: _backgroundColor,
      behavior: behaviour,
      duration: duration,
      elevation: elevation,
      margin: margin,
      padding: padding,
      shape: shape,
      width: width,
    ),
  );
}