showYesNoAlertDialog function

Future<bool?> showYesNoAlertDialog({
  1. required BuildContext context,
  2. required Widget? title,
  3. required Widget? content,
  4. required Widget? yesText,
  5. required Widget? noText,
  6. bool reverseYesNoOrder = false,
  7. bool barrierDismissible = true,
  8. Color? barrierColor = Colors.black54,
  9. String? barrierLabel,
  10. bool useSafeArea = true,
  11. bool useRootNavigator = true,
  12. RouteSettings? routeSettings,
  13. EdgeInsetsGeometry? titlePadding,
  14. TextStyle? titleTextStyle,
  15. EdgeInsetsGeometry contentPadding = kAlertDialogDefaultContentPadding,
  16. TextStyle? contentTextStyle,
  17. List<Widget>? actions,
  18. EdgeInsetsGeometry actionsPadding = EdgeInsets.zero,
  19. MainAxisAlignment? actionsAlignment,
  20. VerticalDirection? actionsOverflowDirection,
  21. double? actionsOverflowButtonSpacing,
  22. EdgeInsetsGeometry? buttonPadding,
  23. Color? backgroundColor,
  24. double? elevation,
  25. String? semanticLabel,
  26. EdgeInsets insetPadding = kDialogDefaultInsetPadding,
  27. Clip clipBehavior = Clip.none,
  28. ShapeBorder? shape,
  29. AlignmentGeometry? alignment,
  30. bool scrollable = false,
  31. void yesOnPressed(
    1. BuildContext
    )?,
  32. void yesOnLongPress(
    1. BuildContext
    )?,
  33. void yesOnHover(
    1. BuildContext,
    2. bool
    )?,
  34. void yesOnFocusChange(
    1. BuildContext,
    2. bool
    )?,
  35. ButtonStyle? yesStyle,
  36. FocusNode? yesFocusNode,
  37. bool yesAutoFocus = false,
  38. Clip yesClipBehavior = Clip.none,
  39. void noOnPressed(
    1. BuildContext
    )?,
  40. void noOnLongPress(
    1. BuildContext
    )?,
  41. void noOnHover(
    1. BuildContext,
    2. bool
    )?,
  42. void noOnFocusChange(
    1. BuildContext,
    2. bool
    )?,
  43. ButtonStyle? noStyle,
  44. FocusNode? noFocusNode,
  45. bool noAutoFocus = false,
  46. Clip noClipBehavior = Clip.none,
})

This is a convenient function to show AlertDialog with given title, content and two action buttons.

Note that you can set yesText or noText to null to hide the specific button. Also note that yesOnPressed and noOnPressed can be used to override the default onPressed callback, but Navigator.pop must be called manually.

Implementation

Future<bool?> showYesNoAlertDialog({
  required BuildContext context,
  required Widget? title,
  required Widget? content,
  required Widget? yesText,
  required Widget? noText,
  bool reverseYesNoOrder = false,
  // showDialog parameters
  bool barrierDismissible = true,
  Color? barrierColor = Colors.black54,
  String? barrierLabel,
  bool useSafeArea = true,
  bool useRootNavigator = true,
  RouteSettings? routeSettings,
  // AlertDialog parameters
  EdgeInsetsGeometry? titlePadding,
  TextStyle? titleTextStyle,
  EdgeInsetsGeometry contentPadding = kAlertDialogDefaultContentPadding,
  TextStyle? contentTextStyle,
  List<Widget>? actions,
  EdgeInsetsGeometry actionsPadding = EdgeInsets.zero,
  MainAxisAlignment? actionsAlignment,
  VerticalDirection? actionsOverflowDirection,
  double? actionsOverflowButtonSpacing,
  EdgeInsetsGeometry? buttonPadding,
  Color? backgroundColor,
  double? elevation,
  String? semanticLabel,
  EdgeInsets insetPadding = kDialogDefaultInsetPadding,
  Clip clipBehavior = Clip.none,
  ShapeBorder? shape,
  AlignmentGeometry? alignment,
  bool scrollable = false,
  // actions parameters
  void Function(BuildContext)? yesOnPressed,
  void Function(BuildContext)? yesOnLongPress,
  void Function(BuildContext, bool)? yesOnHover,
  void Function(BuildContext, bool)? yesOnFocusChange,
  ButtonStyle? yesStyle,
  FocusNode? yesFocusNode,
  bool yesAutoFocus = false,
  Clip yesClipBehavior = Clip.none,
  void Function(BuildContext)? noOnPressed,
  void Function(BuildContext)? noOnLongPress,
  void Function(BuildContext, bool)? noOnHover,
  void Function(BuildContext, bool)? noOnFocusChange,
  ButtonStyle? noStyle,
  FocusNode? noFocusNode,
  bool noAutoFocus = false,
  Clip noClipBehavior = Clip.none,
}) async {
  return await showDialog<bool>(
    context: context,
    builder: (c) {
      var actions = [
        if (yesText != null)
          TextButton(
            child: yesText,
            onPressed: yesOnPressed == null ? () => Navigator.of(c).pop(true) : () => yesOnPressed.call(c),
            onLongPress: yesOnLongPress == null ? null : () => yesOnLongPress.call(c),
            onHover: yesOnHover == null ? null : (v) => yesOnHover.call(c, v),
            onFocusChange: yesOnFocusChange == null ? null : (v) => yesOnFocusChange.call(c, v),
            style: yesStyle,
            focusNode: yesFocusNode,
            autofocus: yesAutoFocus,
            clipBehavior: yesClipBehavior,
          ),
        if (noText != null)
          TextButton(
            child: noText,
            onPressed: noOnPressed == null ? () => Navigator.of(c).pop(false) : () => noOnPressed.call(c),
            onLongPress: noOnLongPress == null ? null : () => noOnLongPress.call(c),
            onHover: noOnHover == null ? null : (v) => noOnHover.call(c, v),
            onFocusChange: noOnFocusChange == null ? null : (v) => noOnFocusChange.call(c, v),
            style: noStyle,
            focusNode: noFocusNode,
            autofocus: noAutoFocus,
            clipBehavior: noClipBehavior,
          ),
      ];
      if (reverseYesNoOrder) {
        actions = actions.reversed.toList();
      }
      return AlertDialog(
        title: title,
        content: content,
        actions: actions,
        // AlertDialog parameters
        titlePadding: titlePadding,
        titleTextStyle: titleTextStyle,
        contentPadding: contentPadding,
        contentTextStyle: contentTextStyle,
        actionsPadding: actionsPadding,
        actionsAlignment: actionsAlignment,
        actionsOverflowDirection: actionsOverflowDirection,
        actionsOverflowButtonSpacing: actionsOverflowButtonSpacing,
        buttonPadding: buttonPadding,
        backgroundColor: backgroundColor,
        elevation: elevation,
        semanticLabel: semanticLabel,
        insetPadding: insetPadding,
        clipBehavior: clipBehavior,
        shape: shape,
        alignment: alignment,
        scrollable: scrollable,
      );
    },
    // showDialog parameters
    barrierDismissible: barrierDismissible,
    barrierColor: barrierColor,
    barrierLabel: barrierLabel,
    useSafeArea: useSafeArea,
    useRootNavigator: useRootNavigator,
    routeSettings: routeSettings,
  );
}