alertDialog function

dynamic alertDialog({
  1. @required BuildContext? context,
  2. @required Widget? title,
  3. @required Widget? content,
  4. String? cancelLabel,
  5. CancelListener? cancelListener,
  6. String? okLabel,
  7. OkListener? okListener,
  8. ThenListener? thenListener,
})

Implementation

alertDialog({
  @required BuildContext? context,
  @required Widget? title,
  @required Widget? content,
  String? cancelLabel,
  CancelListener? cancelListener,
  String? okLabel,
  OkListener? okListener,
  ThenListener? thenListener,
}) {
  if (context == null) {
    return;
  }

  List<Widget> actions = [];

  if (cancelLabel != null && cancelListener != null) {
    actions.add(
      FlatButton(
        key: UniqueKey(),
        child: Text(
          cancelLabel,
          style: TextStyle(color: Colors.black),
        ),
        onPressed: () {
          Navigator.pop(context, cancelLabel);
          cancelListener();
        },
      ),
    );
  }

  if (okLabel != null && okListener != null) {
    actions.add(
      FlatButton(
        key: UniqueKey(),
        child: Text(
          okLabel,
          style: TextStyle(color: Colors.black),
        ),
        onPressed: () {
          Navigator.pop(context, okLabel);
          okListener();
        },
      ),
    );
  }

  showDialog<String>(
    barrierDismissible: false,
    context: context,
    builder: (BuildContext context) => WillPopScope(
      onWillPop: () {
        return Future.value(false);
      },
      child: AlertDialog(
        backgroundColor: Colors.white,
        title: title,
        content: content,
        actions: actions,
      ),
    ),
  ).then((returnVal) {
    if (returnVal != null && thenListener != null) {
      thenListener();
    }
  });
}