dialogWithOptions static method

void dialogWithOptions({
  1. required BuildContext context,
  2. required String title,
  3. required String message,
  4. String textLeftButton = 'OK',
  5. Color? titleTextColor,
  6. Color? messageTextColor,
  7. String textRightButton = 'Cancel',
  8. Function? onPressedLeftButton,
  9. Function? onPressedRightButton,
  10. bool isRightButtonADestructiveAction = true,
  11. double buttonFontSize = 20,
  12. double titleFontSize = 25,
})

Here is the dialog with options, it's useful if you want to pop up a dialog where you can choose between two options. One of them can be set as destructive action.

Implementation

static void dialogWithOptions(
    {required BuildContext context,
      required String title,
      required String message,
      String textLeftButton = 'OK',
      Color? titleTextColor,
      Color? messageTextColor,

      /// You can change the dismiss button text
      String textRightButton = 'Cancel',
      Function? onPressedLeftButton,
      Function? onPressedRightButton,

      /// If set to true, the right button will be considered as a destructive button and will be colored in red
      bool isRightButtonADestructiveAction = true,
      double buttonFontSize = 20,
      double titleFontSize = 25}) {
  if (Platform.isIOS) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        /// Cupertino dialogs
        return CupertinoAlertDialog(
          title: Center(child: new Text(title)),
          content: new Text(message),
          actions: <Widget>[
            CupertinoDialogAction(
              child: Text(
                textLeftButton,
                textAlign: TextAlign.justify,
              ),
              isDefaultAction: true,
              /// This is the default action
              onPressed: onPressedLeftButton != null
                  ? () {
                Navigator.pop(context);
                onPressedLeftButton();
              }
                  : () => Navigator.of(context).pop(),
            ),
            CupertinoDialogAction(
              child: Text(
                textRightButton,
                textAlign: TextAlign.justify,
              ),
              /// If the function is null, we just pop the dialog.
              onPressed: onPressedRightButton != null
                  ? () {
                Navigator.pop(context);
                onPressedRightButton();
              }
                  : () => Navigator.of(context).pop(),
              isDestructiveAction: isRightButtonADestructiveAction,
            ),
          ],
        );
      },
    );
  } else {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Center(
              child: new Text(
                title,
                style: TextStyle(
                    fontSize: titleFontSize,
                    fontWeight: FontWeight.bold,
                    color: titleTextColor),
              )),
          content: new Text(
            message,
            textAlign: TextAlign.justify,
            style: TextStyle(color: messageTextColor),
          ),
          actions: <Widget>[
            new TextButton(
              child: new Text(
                textLeftButton,
                style: TextStyle(fontSize: buttonFontSize),
              ),
              onPressed: onPressedLeftButton != null
                  ? () {
                Navigator.pop(context);
                onPressedLeftButton();
              }
                  : () => Navigator.of(context).pop(),
            ),
            new TextButton(
              child: new Text(
                textRightButton,
                style: TextStyle(
                    fontSize: buttonFontSize,
                    color: isRightButtonADestructiveAction
                        ? Colors.redAccent
                        : Colors.white),
              ),
              /// If the function is null, we just pop the dialog.
              onPressed: onPressedRightButton != null
                  ? () {
                Navigator.pop(context);
                onPressedRightButton();
              } /// If the function is null, we just pop the dialog.
                  : () => Navigator.of(context).pop(),
            ),
          ],
        );
      },
    );
  }
}