Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get callback from custom alert dialog

I need to execute a function after calling a custom alert dialog.

Here you have how am I calling the custom Alert Dialog:

onTap: () async {

                showDialog(
                    barrierColor: Colors.black26,
                    context: context,
                    builder: (context) {
                      return CustomAlertDialog(
                        title: "confirmarborrarusuario".tr(),
                        description: "borrarusuario".tr(),
                        imagen: widget.usuarioInterno.imagen,
                        email: widget.usuarioInterno.email,
                      );
                    });

              },

And here you have the complete code for the Custom Dialog:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import 'package:flutter/material.dart';
 class CustomAlertDialog extends StatefulWidget {
      const CustomAlertDialog({
        Key? key,
        required this.title,
        required this.description,
        required this.imagen,
        required this.email,
      }) : super(key: key);
    
      final String title, description, imagen, email;
    
      @override
      _CustomAlertDialogState createState() => _CustomAlertDialogState();
    }
    
    class _CustomAlertDialogState extends State<CustomAlertDialog> {
      @override
      Widget build(BuildContext context) {
        return Dialog(
          elevation: 0,
          backgroundColor: Color(0xffffffff),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(15.0),
          ),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(height: 15),
                Text(
                  "${widget.title}",
                  style: TextStyle(
                    color: Colors.black38,
                    fontSize: 18.0,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(height: 15),
                widget.imagen.length > 1 ? Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: CircleAvatar(
                    radius: 55,
                    backgroundColor: Colors.red,
                    child: CircleAvatar(
                      radius: 50,
                      backgroundImage: NetworkImage(widget.imagen),
                    ),
                  ),
                ): Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: CircleAvatar(
                    radius: 55,
                    backgroundColor: Colors.red,
                    child: CircleAvatar(
                      radius: 50,
                      backgroundImage: AssetImage('imagenes/nofoto.png'),
                    ),
                  ),
                ),
                SizedBox(height: 20),
                Text("${widget.email}",style: TextStyle(color: Colors.black,fontSize: 16),),
                Divider(
                  height: 1,
                ),
                Container(
                  width: MediaQuery.of(context).size.width,
                  height: 50,
                  child: InkWell(
                    highlightColor: Colors.grey[200],
                    onTap: () {
                      //do somethig
                    },
                    child: Center(
                      child: Text(
                        "${widget.description}",
                        style: TextStyle(
                          fontSize: 18.0,
                          color: Colors.red,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                ),
                Divider(
                  height: 1,
                ),
                Container(
                  width: MediaQuery.of(context).size.width,
                  height: 50,
                  child: InkWell(
                    borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(15.0),
                      bottomRight: Radius.circular(15.0),
                    ),
                    highlightColor: Colors.grey[200],
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: Center(
                      child: Text(
                        "Cancelar",
                        style: TextStyle(
                          fontSize: 16.0,
                          fontWeight: FontWeight.normal,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }

What I need is to get a callback from the dialog telling me if the user has clicked on the first button or not, if the user clicks on the cancel button its ok, the dialog is dismissed and enough

>Solution :

This way u can send callback modify it according to your need.

onTap: () async {

                showDialog(
                    barrierColor: Colors.black26,
                    context: context,
                    builder: (context) {
                      return CustomAlertDialog(
                        title: "confirmarborrarusuario".tr(),
                        description: "borrarusuario".tr(),
                        imagen: widget.usuarioInterno.imagen,
                        email: widget.usuarioInterno.email,
                        callback:(value}{
                            // value from call back whatever u send when u tap on widget 
                            print(value);
                         }
                      );
                    });

              },

Updated Code

 class CustomAlertDialog extends StatefulWidget {
      const CustomAlertDialog({
        Key? key,
        required this.title,
        required this.description,
        required this.imagen,
        required this.email,
               this.callback,
      }) : super(key: key);
    
      final String title, description, imagen, email;
      final Function callback,
    
      @override
      _CustomAlertDialogState createState() => _CustomAlertDialogState();
    }
    
    class _CustomAlertDialogState extends State<CustomAlertDialog> {
      @override
      Widget build(BuildContext context) {
        return Dialog(
          elevation: 0,
          backgroundColor: Color(0xffffffff),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(15.0),
          ),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(height: 15),
                Text(
                  "${widget.title}",
                  style: TextStyle(
                    color: Colors.black38,
                    fontSize: 18.0,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(height: 15),
                widget.imagen.length > 1 ? Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: CircleAvatar(
                    radius: 55,
                    backgroundColor: Colors.red,
                    child: CircleAvatar(
                      radius: 50,
                      backgroundImage: NetworkImage(widget.imagen),
                    ),
                  ),
                ): Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: CircleAvatar(
                    radius: 55,
                    backgroundColor: Colors.red,
                    child: CircleAvatar(
                      radius: 50,
                      backgroundImage: AssetImage('imagenes/nofoto.png'),
                    ),
                  ),
                ),
                SizedBox(height: 20),
                Text("${widget.email}",style: TextStyle(color: Colors.black,fontSize: 16),),
                Divider(
                  height: 1,
                ),
                Container(
                  width: MediaQuery.of(context).size.width,
                  height: 50,
                  child: InkWell(
                    highlightColor: Colors.grey[200],
                    onTap: () {
                      //do somethig 
                     
                    widget.callback.call('pass here value');
                    },
                    child: Center(
                      child: Text(
                        "${widget.description}",
                        style: TextStyle(
                          fontSize: 18.0,
                          color: Colors.red,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                ),
                Divider(
                  height: 1,
                ),
                Container(
                  width: MediaQuery.of(context).size.width,
                  height: 50,
                  child: InkWell(
                    borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(15.0),
                      bottomRight: Radius.circular(15.0),
                    ),
                    highlightColor: Colors.grey[200],
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: Center(
                      child: Text(
                        "Cancelar",
                        style: TextStyle(
                          fontSize: 16.0,
                          fontWeight: FontWeight.normal,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading