0

I'm trying to use CupertinoAlertDialog on my profile page. It works just fine on my other page, but not the profile page.

Here is my code:

showCupertinoModalPopup(
context: context,
  builder: (context) => CupertinoActionSheet(
    actions: [
      image != null ? CupertinoActionSheetAction(
        child: Text('Remove photo',
          style: TextStyle(
              color: Colors.red
          ),),
        onPressed: () {
          Navigator.pop(context);
          _openDialog(context);
        },
      )
void _openDialog(ctx) {
  showCupertinoDialog(
      context: ctx,
      builder: (_) => CupertinoAlertDialog(
        title: Text("Delete Chat"),
        content: Text("Messages will be removed from this device only."),
        actions: [
          // Close the dialog
          // You can use the CupertinoDialogAction widget instead
          CupertinoButton(
              child: Text('Cancel',
                style: TextStyle(
                  color: Color(0xFF2481CF)
                ),
              ),
              onPressed: () {
                Navigator.of(ctx).pop();
              }),
          CupertinoButton(
            child: Text('Delete',
              style: TextStyle(
                color: Colors.red
              ),
            ),
            onPressed: () {
              Navigator.of(ctx).pop();
            },
          )
        ],
      ));
}

And this is the error:

enter image description here

Is there any solutions?

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
aufa
  • 273
  • 3
  • 15

1 Answers1

2

Try this

Navigator.of(context).pop(false);

instead of

Navigator.of(ctx).pop();

In your code.its working for me.

Your Alert:

    openDialog(BuildContext context) {
    showCupertinoDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => CupertinoAlertDialog(
        title: Text('Delete Chat'),
        content: Text('Messages will be removed from this device only.'),
        actions: <Widget>[
          CupertinoButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: Text(
              'Cancel',
              style: TextStyle(
                color: Color(0xFF2481CF),
              ),
            ),
          ),
          CupertinoButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: Text(
              'Delete',
              style: TextStyle(color: Colors.red),
            ),
          ),
        ],
      ),
    );
  }

Your Widget:

 ElevatedButton(
        onPressed: () {
          showCupertinoModalPopup(
            context: context,
            builder: (context) => CupertinoActionSheet(
              actions: [
                CupertinoActionSheetAction(
                  child: Text(
                    'Remove photo',
                    style: TextStyle(color: Colors.red),
                  ),
                  onPressed: () {
                    Navigator.pop(context);
                    openDialog(context);
                  },
                ),
              ],
            ),
          );
        },
        child: Text('Click me'),
        style: ElevatedButton.styleFrom(
          primary: Colors.red,
        ),
      ),
Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40