wrapInYesCancelConfirmDialog static method

void wrapInYesCancelConfirmDialog(
  1. BuildContext context,
  2. String title, {
  3. String? contentString,
  4. Widget? contentWidget,
  5. String action1Text = 'Yes',
  6. required VoidCallback action1,
  7. Color? action1ButtonFgColor,
  8. String cancelActionText = 'Cancel',
  9. bool preActionValidation()?,
})

Shows a confirmation dialog with Yes/Cancel buttons.

Parameters:

  • context: The build context
  • title: Dialog title
  • contentString: Optional content text (either this or contentWidget must be provided)
  • contentWidget: Optional content widget (either this or contentString must be provided)
  • action1Text: Text for confirmation button. Defaults to 'Yes'
  • action1: Callback when confirmation button is pressed
  • action1ButtonFgColor: Optional foreground color for confirmation button
  • cancelActionText: Text for cancel button. Defaults to 'Cancel'
  • preActionValidation: Optional validation function called before executing action1

Throws an error if neither contentString nor contentWidget is provided.

Implementation

static void wrapInYesCancelConfirmDialog(BuildContext context, String title,
    {String? contentString,
    Widget? contentWidget,
    String action1Text = 'Yes',
    required VoidCallback action1,
    Color? action1ButtonFgColor,
    String cancelActionText = 'Cancel',
    bool Function()? preActionValidation}) {
  if (contentString.isNullOrEmpty && contentWidget == null) {
    throw 'Either contentString or contentWidget should be supplied';
  }

  Widget content =
      contentString != null ? Text(contentString) : contentWidget!;

  showDialog(
      context: context,
      builder: (ctx) {
        return AlertDialog(
          title: Text(title),
          content: content,
          actions: [
            TextButton(
                onPressed: () {
                  bool execute =
                      preActionValidation == null || preActionValidation();
                  if (execute) {
                    action1();
                    Navigator.pop(ctx);
                  }
                },
                style: ButtonStyle(
                    foregroundColor:
                        WidgetStatePropertyAll(action1ButtonFgColor)),
                child: Text(action1Text)),
            TextButton(
                onPressed: () => Navigator.pop(ctx),
                child: Text(cancelActionText)),
          ],
        );
      });
}