Home > Software engineering >  Get callback from custom alert dialog
Get callback from custom alert dialog

Time:03-04

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:

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

CodePudding user response:

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,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
  • Related