showOkCancelAlertDialog function

  1. @useResult
Future<OkCancelResult> showOkCancelAlertDialog({
  1. required BuildContext context,
  2. String? title,
  3. String? message,
  4. String? okLabel,
  5. String? cancelLabel,
  6. OkCancelAlertDefaultType? defaultType,
  7. bool isDestructiveAction = false,
  8. bool barrierDismissible = true,
  9. @Deprecated('Use `style` instead.') AdaptiveStyle? alertStyle,
  10. AdaptiveStyle? style,
  11. @Deprecated('Use `ios` instead. Will be removed in v2.') bool useActionSheetForCupertino = false,
  12. bool useActionSheetForIOS = false,
  13. bool useRootNavigator = true,
  14. VerticalDirection actionsOverflowDirection = VerticalDirection.up,
  15. bool fullyCapitalizedForMaterial = true,
  16. bool canPop = true,
  17. PopInvokedCallback? onPopInvoked,
  18. AdaptiveDialogBuilder? builder,
  19. RouteSettings? routeSettings,
})

Show OK/Cancel alert dialog, whose appearance is adaptive according to platform

This is convenient wrapper of showAlertDialog. barrierDismissible (default: true) only works for material style, and if it is set to false, pressing OK or Cancel buttons is only way to close alert. defaultType only works for cupertino style and if it is specified OK or Cancel button label will be changed to bold. actionsOverflowDirection works only for Material style currently.

Implementation

@useResult
Future<OkCancelResult> showOkCancelAlertDialog({
  required BuildContext context,
  String? title,
  String? message,
  String? okLabel,
  String? cancelLabel,
  OkCancelAlertDefaultType? defaultType,
  bool isDestructiveAction = false,
  bool barrierDismissible = true,
  @Deprecated('Use `style` instead.') AdaptiveStyle? alertStyle,
  AdaptiveStyle? style,
  @Deprecated('Use `ios` instead. Will be removed in v2.')
  bool useActionSheetForCupertino = false,
  bool useActionSheetForIOS = false,
  bool useRootNavigator = true,
  VerticalDirection actionsOverflowDirection = VerticalDirection.up,
  bool fullyCapitalizedForMaterial = true,
  bool canPop = true,
  PopInvokedCallback? onPopInvoked,
  AdaptiveDialogBuilder? builder,
  RouteSettings? routeSettings,
}) async {
  final theme = Theme.of(context);
  final adaptiveStyle = style ?? AdaptiveDialog.instance.defaultStyle;
  final isMaterial = adaptiveStyle.isMaterial(theme);
  String defaultCancelLabel() {
    final label = MaterialLocalizations.of(context).cancelButtonLabel;
    return isMaterial ? label : label.capitalizedForce;
  }

  final result = await showAlertDialog<OkCancelResult>(
    routeSettings: routeSettings,
    context: context,
    title: title,
    message: message,
    barrierDismissible: barrierDismissible,
    style: alertStyle ?? style,
    useActionSheetForIOS: useActionSheetForCupertino || useActionSheetForIOS,
    useRootNavigator: useRootNavigator,
    actionsOverflowDirection: actionsOverflowDirection,
    fullyCapitalizedForMaterial: fullyCapitalizedForMaterial,
    canPop: canPop,
    onPopInvoked: onPopInvoked,
    builder: builder,
    actions: [
      AlertDialogAction(
        label: cancelLabel ?? defaultCancelLabel(),
        key: OkCancelResult.cancel,
        isDefaultAction: defaultType == OkCancelAlertDefaultType.cancel,
      ),
      AlertDialogAction(
        label: okLabel ?? MaterialLocalizations.of(context).okButtonLabel,
        key: OkCancelResult.ok,
        isDefaultAction:
            defaultType == null || defaultType == OkCancelAlertDefaultType.ok,
        isDestructiveAction: isDestructiveAction,
      ),
    ],
  );
  return result ?? OkCancelResult.cancel;
}