alert static method

Future<String?> alert({
  1. String? title,
  2. String? message,
  3. List<NativeItem> actions = const [],
  4. BuildContext? context,
})

Shows a native alert and returns the id of the tapped action (NativeItem.id, or its title).

actions default to a single "OK". Roles map to alert action styles (destructive is red, cancel is bold).

final choice = await NativeDialog.alert(
  title: 'Delete photo?',
  message: 'This cannot be undone.',
  actions: const [
    NativeItem(title: 'Delete', role: NativeItemRole.destructive),
    NativeItem(title: 'Cancel', role: NativeItemRole.cancel),
  ],
);

Implementation

static Future<String?> alert({
  String? title,
  String? message,
  List<NativeItem> actions = const [],
  BuildContext? context,
}) async {
  if (_isIOS) return _show('alert', title, message, actions, context);
  if (context == null) return null;
  final buttons = actions.isEmpty ? const [NativeItem(title: 'OK')] : actions;
  return showDialog<String>(
    context: context,
    builder: (context) => AlertDialog(
      title: title == null ? null : Text(title),
      content: message == null ? null : Text(message),
      actions: [
        for (final action in buttons)
          TextButton(
            onPressed: action.enabled
                ? () => Navigator.pop(context, _idOf(action))
                : null,
            style: action.role == NativeItemRole.destructive
                ? TextButton.styleFrom(
                    foregroundColor: Theme.of(context).colorScheme.error)
                : null,
            child: Text(action.title),
          ),
      ],
    ),
  );
}