Home > database >  How to automatically close a dialog box without clicking in flutter?
How to automatically close a dialog box without clicking in flutter?

Time:07-26

I have a dialog box, when I click on the Send Again button, I make a request to the server and, if successful, another _emailSentDialog dialog box opens from above. How can I make the first EmailVerifyDialog automatically close when the second _emailSentDialog is opened?

class EmailVerifyDialog extends StatelessWidget {
  final CurrentEmailCubit cubit;
  const EmailVerifyDialog({Key? key, required this.cubit}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return AlertDialogGradientBorder(
      height: size.height * .5,
      child: SizedBox(
        width: size.width,
        child: Padding(
          padding: const EdgeInsets.only(bottom: 35),
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 45),
                child: RichText(
                  textAlign: TextAlign.center,
                  text: TextSpan(
                    children: [
                      WidgetSpan(
                        child: FittedBox(
                          child: Row(
                            children: [
                              const Text(
                                'Didn’t recieve a link? ',
                                style: constants.Styles.smallBookTextStyleWhite,
                              ),
                              TextButton(
                                style: TextButton.styleFrom(
                                    padding: EdgeInsets.zero),
                                onPressed: () async {
                                  await cubit
                                      .sendConfirmationCode(
                                          email: cubit.currentEmail)
                                      .then((value) => {
                                            if (value)
                                              {
                                                _emailSentDialog(context),
                                              }
                                            else
                                              {
                                                _errorNotification(context),
                                              }
                                          });
                                  log(cubit.currentEmail);
                                },
                                child: const Text(
                                  'Send Again',
                                  style: constants
                                      .Styles.smallBookUnderlineTextStyleWhite,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _closeDialog(BuildContext context) {
    Navigator.pop(context);
  }

  void _emailSentDialog(context) async {
    showDialog(
      context: context,
      builder: (BuildContext context) => const EmailSentDialog(),
    );
  }

CodePudding user response:

I was referring this way

onPressed: () async {
  setState(() {
    text = "checking on Server";
  });
  final showSecondDialog = await serverResult();
  if (showSecondDialog) {
    if (!mounted) return;
    Navigator.of(context).pop();
    showDialog2(context);
  }
},
class TestDialogActivity extends StatefulWidget {
  const TestDialogActivity({Key? key}) : super(key: key);

  @override
  State<TestDialogActivity> createState() => _TestDialogActivityState();
}

class _TestDialogActivityState extends State<TestDialogActivity> {
  showDialog2(context) async {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextButton(
                onPressed: () async {
                  Navigator.of(context).pop();
                },
                child: Text(" close Second dialog "))
          ],
        ),
      ),
    );
  }

  Future<bool> serverResult() async {
    await Future.delayed(Duration(seconds: 1));
    return true;
  }

  showDialog1(context) async {
    String text = "sendAgain";
    showDialog(
      context: context,
      builder: (context) => StatefulBuilder(
        builder: (context, setState) => AlertDialog(
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextButton(
                  onPressed: () async {
                    setState(() {
                      text = "checking on Server";
                    });
                    final showSecondDialog = await serverResult();
                    if (showSecondDialog) {
                      if (!mounted) return;
                      Navigator.of(context).pop();
                      showDialog2(context);
                    }
                  },
                  child: Text("$text"))
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () => showDialog1(context),
      ),
    );
  }
}
  • Related