showAlert static method

Future<void> showAlert(
  1. BuildContext context,
  2. SecurityEvent event, {
  3. SecurityAlertConfig config = const SecurityAlertConfig(),
})

Implementation

static Future<void> showAlert(
  BuildContext context,
  SecurityEvent event, {
  SecurityAlertConfig config = const SecurityAlertConfig(),
}) async {
  if (config.mode == SecurityAlertMode.silent) {
    return;
  }

  final String message =
      config.customMessageByType[event.type] ?? _defaultMessageFor(event);

  if (config.customHandler != null) {
    await config.customHandler!(context, event, message);
    return;
  }

  if (!context.mounted) {
    return;
  }

  switch (config.mode) {
    case SecurityAlertMode.silent:
      return;
    case SecurityAlertMode.snackbar:
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text(message)));
      return;
    case SecurityAlertMode.toast:
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(message),
          duration: const Duration(milliseconds: 1200),
          behavior: SnackBarBehavior.floating,
          margin: const EdgeInsets.only(left: 24, right: 24, bottom: 52),
        ),
      );
      return;
    case SecurityAlertMode.dialog:
      await showDialog<void>(
        context: context,
        builder: (dialogContext) => AlertDialog(
          title: const Text('Security Alert'),
          content: Text(message),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.of(dialogContext).pop(),
              child: const Text('OK'),
            ),
          ],
        ),
      );
      return;
  }
}