show static method
Future<int?>
show({
- required String title,
- String? message,
- required List<
CNAlertAction> actions, - int? preferredActionIndex,
Shows a native alert dialog.
title: The title of the alert (required).message: Optional informative text that adds value.actions: List of action buttons (up to 3 recommended).preferredActionIndex: Index of the preferred (default) action.
Returns the index of the action that was tapped, or null if dismissed.
Implementation
static Future<int?> show({
required String title,
String? message,
required List<CNAlertAction> actions,
int? preferredActionIndex,
}) async {
if (actions.isEmpty) {
throw ArgumentError('Alert must have at least one action');
}
if (preferredActionIndex != null &&
(preferredActionIndex < 0 || preferredActionIndex >= actions.length)) {
throw ArgumentError('preferredActionIndex out of range');
}
try {
final result = await _channel.invokeMethod<int>('showAlert', {
'title': title,
'message': message,
'actions': actions.map((a) => a.toMap()).toList(),
'preferredActionIndex': preferredActionIndex,
});
// Execute the callback for the selected action
if (result != null && result >= 0 && result < actions.length) {
actions[result].onPressed?.call();
}
return result;
} catch (e) {
print('Error showing alert: $e');
return null;
}
}