appSnackBar function

void appSnackBar({
  1. required BuildContext context,
  2. required String message,
  3. Color? backgroundColor,
  4. Color? textColor,
  5. double? fontSize,
  6. SnackBarBehavior? behavior,
  7. Duration duration = const Duration(seconds: 3),
  8. EdgeInsetsGeometry? margin,
  9. double? elevation,
  10. SnackBarAction? action,
  11. IconData? icon,
  12. Color? iconColor,
  13. double? iconSize,
})

A helper function to easily show customizable SnackBars.

Supports icons, actions, and floating or fixed behavior.

Implementation

void appSnackBar({
  required BuildContext context,
  required String message,
  Color? backgroundColor,
  Color? textColor,
  double? fontSize,
  SnackBarBehavior? behavior,
  Duration duration = const Duration(seconds: 3),
  EdgeInsetsGeometry? margin,
  double? elevation,
  SnackBarAction? action,
  IconData? icon,
  Color? iconColor,
  double? iconSize,
}) {
  final SnackBarBehavior resolvedBehavior =
      behavior ?? SnackBarBehavior.floating;

  final Widget content = Row(
    mainAxisSize: MainAxisSize.min,
    children: [
      if (icon != null) ...[
        Icon(
          icon,
          color: iconColor ?? Colors.white,
          size: iconSize ?? 20,
        ),
        const SizedBox(width: 8),
      ],
      Flexible(
        child: Text(
          message,
          style: TextStyle(
            color: textColor ?? Colors.white,
            fontSize: fontSize ?? 14,
          ),
        ),
      ),
    ],
  );

  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      content: content,
      backgroundColor: backgroundColor ?? Colors.blueAccent,
      behavior: resolvedBehavior,
      duration: duration,
      margin: margin,
      elevation: elevation,
      action: action,
    ),
  );
}