defaultDialog static method

dynamic defaultDialog({
  1. required BuildContext context,
  2. String title = 'Removal Confirmation',
  3. Widget? content,
  4. String cancelText = 'Dismiss',
  5. String confirmText = 'Remove',
  6. dynamic onCancel()?,
  7. dynamic onConfirm()?,
  8. bool hideActions = false,
  9. bool canPop = true,
})

A default dialog with customizable title, content, buttons, and actions.

  • context: The BuildContext in which the dialog will be displayed.
  • title: The title of the dialog (default: 'Removal Confirmation').
  • content: The content widget to display inside the dialog. Defaults to a confirmation message.
  • cancelText: The text to display on the cancel button (default: 'Dismiss').
  • confirmText: The text to display on the confirm button (default: 'Remove').
  • onCancel: A callback function for the cancel button (optional). If not provided, the dialog will be dismissed.
  • onConfirm: A callback function for the confirm button (optional).
  • hideActions: A flag to hide action buttons (optional; default: false).
  • canPop: A flag to enable or disable dismissing the dialog by tapping outside (default: true).

Implementation

static defaultDialog({
  required BuildContext context,
  String title = 'Removal Confirmation', // Default title
  Widget? content, // Custom content for the dialog
  String cancelText = 'Dismiss', // Default cancel button text
  String confirmText = 'Remove', // Default confirm button text
  Function()? onCancel, // Custom cancel action
  Function()? onConfirm, // Custom confirm action
  bool hideActions = false, // Flag to hide buttons
  bool canPop = true, // Flag to control dismissing the dialog by tapping outside
}) {
  showDialog(
    context: context,
    barrierDismissible: canPop,
    builder: (BuildContext context) {
      return PopScope(
        canPop: canPop,
        child: AlertDialog(
          backgroundColor: TColors().lightBackground,
          title: TTextWithIcon(text: title, icon: Iconsax.box),
          content: Container(
            constraints: BoxConstraints(
              maxWidth: TDeviceUtils.isDesktopScreen(context) ? TDeviceUtils.getScreenWidth(context) * 0.7 :TDeviceUtils.getScreenWidth(context) * 0.9 ,
              maxHeight: TDeviceUtils.isDesktopScreen(context) ? TDeviceUtils.getScreenHeight(context) * 0.6 : TDeviceUtils.getScreenHeight(context) * 0.8,
            ),
            child: content ??
                Text(
                  'Removing this data will delete all related data. Are you sure?',
                  style: Theme.of(context).textTheme.bodyMedium, // Default text style
                ),
          ),
          actions: hideActions
              ? null
              : [
                  // Cancel button action, dismisses the dialog or runs custom action
                  OutlinedButton(
                    onPressed: onCancel ?? () => Navigator.of(context).pop(),
                    child: Text(cancelText),
                  ),
                  if (onConfirm != null)
                    // If onConfirm is provided, show the confirm button
                    ElevatedButton(
                      onPressed: onConfirm,
                      child: Text(confirmText),
                    ),
                ],
        ),
      );
    },
  );
}