marcos930807 / awesomeDialogs

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

Pass function (closure) to btnOkOnPress #110

Closed robman70 closed 2 years ago

robman70 commented 2 years ago

I have a problem passing a function to btnOkOnPress.

In a previous version (2.1.3) I could use this code:

  static questionDialog(BuildContext context, String title, String text, Function onPress) async {
    AwesomeDialog(
        context: context,
        dialogType: DialogType.QUESTION,
        headerAnimationLoop: false,
        animType: AnimType.BOTTOMSLIDE,
        title: title,
        desc: text,
        buttonsTextStyle: const TextStyle(color: Colors.black),
        showCloseIcon: true,
        btnCancelText: Locale.undo.toCapitalized(),
        btnCancelOnPress: () {},
        btnOkText: Locale.OK.toCapitalized(),
        btnOkOnPress: onPress,
    ).show();
  }

Then, inside a button, I called the previous function:

  onPressed: () async {
      await global.Utils.questionDialog(
        context,
        global.Locale.attention.toUpperCase(),
        global.Locale.confirmSaveCannotUndo,
        () async {
          ... some code ...
        }
      );
  },

And all worked perfectly.

Using the current version (2.2.1), VS Code shows an error: "The argument type 'Function' can't be assigned to the parameter type 'dynamic Function()?" (refers to 'btnOkOnPress: onPress'), so I tried to modify the function declaration:

    static questionDialog(BuildContext context, String title, String text, Function onPress) async {
        await AwesomeDialog(
            ... (no changes)...
            btnOkOnPress: () async => onPress,
        ).show();
    }

The error disappeared, but the code inside 'onPress' does not run.

What am I doing wrong?

Thanks in advance

OutdatedGuy commented 2 years ago

Yo @robman70, if your onPress function doesn't take any arguments, then change the parameter type of onPress fn in questionDialog to Function() or VoidCallback

i.e.

static questionDialog(BuildContext context, String title, String text, Function() onPress) async {

or

static questionDialog(BuildContext context, String title, String text, VoidCallback onPress) async {
robman70 commented 2 years ago

@OutdatedGuy, thank you for your response but unfortunately none of your solutions worked for me

robman70 commented 2 years ago

@OutdatedGuy , sorry... my error:

static questionDialog(BuildContext context, String title, String text, Function() onPress) async {

worked perfectly.

Thank you!

OutdatedGuy commented 2 years ago

Using the current version (2.2.1), VS Code shows an error: "The argument type 'Function' can't be assigned to the parameter type 'dynamic Function()?" (refers to 'btnOkOnPress: onPress'), so I tried to modify the function declaration:

  static questionDialog(BuildContext context, String title, String text, Function onPress) async {
      await AwesomeDialog(
          ... (no changes)...
          btnOkOnPress: () async => onPress,
      ).show();
  }

The error disappeared, but the code inside 'onPress' does not run.

Btw, I just realized that here you aren't calling the function onPress but just returning it. It should have been like this instead.

static questionDialog(BuildContext context, String title, String text, Function onPress) async {
  await AwesomeDialog(
      ... (no changes)...
      btnOkOnPress: () => onPress(),
  ).show();
}

or

static questionDialog(BuildContext context, String title, String text, Function onPress) async {
  await AwesomeDialog(
      ... (no changes)...
      btnOkOnPress: () {
            onPress();
          },
  ).show();
}
robman70 commented 2 years ago

Yes. Now (thanks to your previous tip) it just works with:

static questionDialog(BuildContext context, String title, String text, Function() onPress) async {
    await AwesomeDialog(
        ...
        btnOkOnPress: onPress,
    ).show();
}