dialog method

Future<void> dialog(
  1. dynamic content, {
  2. String? title,
  3. List<String> buttons = const [],
  4. String? cancelButton,
  5. dynamic onDone(
    1. String
    )?,
  6. Color? positiveTitleColor,
  7. Color? cancelTitleColor,
  8. double? fontSize,
  9. bool barrierDismissible = true,
})

Implementation

Future<void> dialog(dynamic content, {String? title, List<String> buttons = const [], String? cancelButton, Function(String)? onDone, Color? positiveTitleColor, Color? cancelTitleColor, double? fontSize, bool barrierDismissible = true}) async {
  List<Widget> arrWidget = [];

  if (buttons.isEmpty) {
    cancelButton ??= translations.ok;
  }

  if (cancelButton.isNotBlank) {
    if (isApple) {
      CupertinoDialogAction action = CupertinoDialogAction(
        isDefaultAction: true,
        textStyle: TextStyle(color: cancelTitleColor, fontSize: fontSize),
        onPressed: pop,
        child: cancelButton!.asText(),
      );

      arrWidget.add(action);
    } else {
      TextButton action = TextButton(
        style: TextButton.styleFrom(
          foregroundColor: cancelTitleColor,
          textStyle: TextStyle(
            fontSize: fontSize,
          ),
        ),
        onPressed: pop,
        child: cancelButton!.asText(),
      );
      arrWidget.add(action);
    }
  }

  for (var buttonTitle in buttons) {
    late Widget action;

    if (isApple) {
      action = CupertinoDialogAction(
        isDefaultAction: true,
        textStyle: TextStyle(color: positiveTitleColor, fontSize: fontSize),
        onPressed: () {
          if (onDone != null) {
            onDone(buttonTitle);
          }
          pop();
        },
        child: buttonTitle.asText(),
      );
    } else {
      action = TextButton(
        style: TextButton.styleFrom(
          foregroundColor: positiveTitleColor,
          textStyle: TextStyle(
            fontSize: fontSize,
          ),
        ),
        child: buttonTitle.asText(),
        onPressed: () {
          if (onDone != null) {
            onDone(buttonTitle);
          }
          pop();
        },
      );
    }
    arrWidget.add(action);
  }

  return await showDialog(
      barrierDismissible: barrierDismissible,
      context: this,
      builder: (BuildContext context) {
        if (isApple) {
          return CupertinoAlertDialog(
            title: title?.asText(),
            content: forceWidget(content),
            actions: arrWidget,
          );
        } else {
          return AlertDialog(
            title: title?.asText(),
            content: forceWidget(content),
            actions: arrWidget,
          );
        }
      });
}