snack method

void snack(
  1. String message, {
  2. bool success = true,
  3. bool clearAll = true,
  4. int ms = 2000,
  5. String? actionText,
  6. TextStyle? messageStyle,
  7. TextStyle? actionStyle,
  8. VoidCallback? onAction,
  9. bool zeroMargin = true,
  10. Color? textColor,
})

Displays a customizable SnackBar.

Supports optional action, styles, auto-clear, and margin control.

Implementation

void snack(
  String message, {
  bool success = true,
  bool clearAll = true,
  int ms = 2000,
  String? actionText,
  TextStyle? messageStyle,
  TextStyle? actionStyle,
  VoidCallback? onAction,
  bool zeroMargin = true,
  Color? textColor,
}) {
  if (!mounted) return;

  if (clearAll) {
    ScaffoldMessenger.of(this).clearSnackBars();
  }

  ScaffoldMessenger.of(this).showSnackBar(
    SnackBar(
      elevation: 0,
      behavior: SnackBarBehavior.floating,
      duration: Duration(milliseconds: ms),
      margin:
          zeroMargin
              ? const EdgeInsets.symmetric(horizontal: 0, vertical: 0)
              : null,
      content: Row(
        children: [
          Expanded(
            child: Text(
              message,
              style: TextStyle(
                color: textColor ?? Colors.black,
              ).merge(messageStyle),
            ),
          ),
          if (actionText != null)
            Padding(
              padding: const EdgeInsets.only(left: 8),
              child: GestureDetector(
                onTap: () {
                  ScaffoldMessenger.of(this).clearSnackBars();
                  onAction?.call();
                },
                child: Text(
                  actionText,
                  style: TextStyle(
                    color: Colors.blue,
                    fontWeight: FontWeight.w600,
                  ).merge(actionStyle),
                ),
              ),
            ),
        ],
      ),
    ),
  );
}