showNiceToast function

void showNiceToast(
  1. BuildContext context, {
  2. required String message,
  3. NiceToastSeverity severity = NiceToastSeverity.info,
  4. Duration duration = const Duration(seconds: 3),
  5. String? actionLabel,
  6. VoidCallback? onAction,
})

Show a NiceToDev-styled toast notification.

Equivalent of React's NiceFeedback toast.

Implementation

void showNiceToast(
  BuildContext context, {
  required String message,
  NiceToastSeverity severity = NiceToastSeverity.info,
  Duration duration = const Duration(seconds: 3),
  String? actionLabel,
  VoidCallback? onAction,
}) {
  final theme = NiceTheme.of(context);
  final color = switch (severity) {
    NiceToastSeverity.info => theme.colors.info,
    NiceToastSeverity.success => theme.colors.success,
    NiceToastSeverity.warning => theme.colors.warning,
    NiceToastSeverity.error => theme.colors.error,
  };
  final icon = switch (severity) {
    NiceToastSeverity.info => Icons.info_outline,
    NiceToastSeverity.success => Icons.check_circle_outline,
    NiceToastSeverity.warning => Icons.warning_amber_outlined,
    NiceToastSeverity.error => Icons.error_outline,
  };

  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      content: Row(
        children: [
          Icon(icon, color: Colors.white, size: 20),
          const SizedBox(width: 12),
          Expanded(
            child: Text(
              message,
              style: theme.typography.body.copyWith(color: Colors.white),
            ),
          ),
        ],
      ),
      backgroundColor: color,
      duration: duration,
      behavior: SnackBarBehavior.floating,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(theme.borderRadius),
      ),
      margin: EdgeInsets.all(theme.spacing * 2),
      action: actionLabel != null
          ? SnackBarAction(
              label: actionLabel,
              textColor: Colors.white,
              onPressed: onAction ?? () {},
            )
          : null,
    ),
  );
}