show method

void show({
  1. required String message,
  2. Duration duration = const Duration(seconds: 3),
  3. SnackbarType type = SnackbarType.info,
  4. String? actionLabel,
  5. VoidCallback? onAction,
  6. EdgeInsets padding = const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  7. double elevation = 6,
  8. Color? backgroundColor,
  9. TextStyle? textStyle,
  10. SnackBarBehavior behavior = SnackBarBehavior.floating,
  11. ShapeBorder? shape,
  12. Alignment alignment = Alignment.bottomCenter,
})

Implementation

void show({
  required String message,
  Duration duration = const Duration(seconds: 3),
  SnackbarType type = SnackbarType.info,
  String? actionLabel,
  VoidCallback? onAction,
  EdgeInsets padding = const EdgeInsets.symmetric(
    horizontal: 16,
    vertical: 12,
  ),
  double elevation = 6,
  Color? backgroundColor,
  TextStyle? textStyle,
  SnackBarBehavior behavior = SnackBarBehavior.floating,
  ShapeBorder? shape,
  Alignment alignment = Alignment.bottomCenter,
}) {
  final color = backgroundColor ?? _getColor(type);
  final style = textStyle ?? const TextStyle(color: Colors.white);

  final snackBar = SnackBar(
    content: Text(message, style: style),
    duration: duration,
    behavior: behavior,
    elevation: elevation,
    shape: shape ??
        RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
    backgroundColor: color,
    action: actionLabel != null
        ? SnackBarAction(
            label: actionLabel,
            textColor: Colors.white,
            onPressed: onAction ?? () {},
          )
        : null,
    margin: const EdgeInsets.all(12),
  );

  ScaffoldMessenger.of(context).clearSnackBars();
  ScaffoldMessenger.of(context).showSnackBar(snackBar);
}