marcos930807 / awesomeDialogs

A new Flutter package project for simple a awesome dialogs
Other
339 stars 110 forks source link

returning a Future<bool?> from an AwesomeDialog instance #105

Closed murad-alm closed 2 years ago

murad-alm commented 2 years ago

Hi there, thanks for the awesome package!

I am trying to return a bool? from the AwesomeDialog. The function is supposed to return true on OK press or false on CANCEL. When the Dialog is dismissed (via back press or a click outside), null should be returned.

I tried doing it this way but unfortunately, it didn't work..

Future<bool?> _askForImageSource(BuildContext context) async {
    return await AwesomeDialog(
      context: context,
      dialogType: DialogType.QUESTION,
      animType: AnimType.SCALE,
      title: 'Some Title'
      btnCancelText: 'Camera',
      btnOkText: 'Gallery',
      desc: 'Some Description',
      btnCancelOnPress: () {
        return false;
      },
      btnOkOnPress: () {
        return true;
      },
      autoDismiss: false,
      onDissmissCallback: (dismissType) {
        Navigator.of(context).pop(dismissType == DismissType.BTN_OK);
        return null;
      },
    ).show() as bool?;
  }

Thanks again! edit: code

murad-alm commented 2 years ago

Trying the following code, but the return statements are not being executed for some reason.

onDissmissCallback: (dismissType) {
        Navigator.of(context).pop();
        log(dismissType.toString());
        if (dismissType == DismissType.BTN_OK) {
          return true;
        } else if (dismissType == DismissType.BTN_CANCEL) {
          return false;
        } else {
          return null;
        }
      },
murad-alm commented 2 years ago

I got it now. As I said in the previous comment, the return statements didn't get executed for some reason, so I figured out this:

Future<bool?> _askForImageSource(BuildContext context) async {
    bool? source = null;
    await AwesomeDialog(
      context: context,
      dialogType: DialogType.QUESTION,
      animType: AnimType.SCALE,
      title: 'Some Title'
      btnCancelText: 'Camera',
      btnOkText: 'Gallery',
      desc: 'Some Description',
      btnCancelOnPress: () {},
      btnOkOnPress: () {},
      autoDismiss: false,
      onDissmissCallback: (dismissType) {
        Navigator.of(context).pop();
        if (dismissType == DismissType.BTN_OK) {
          source = true;
        } else if (dismissType == DismissType.BTN_CANCEL) {
          source = false;
        } 
     }).show();
    return source;
  }