2

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

Ravindra S. Patil
15.3k5 gold badges22 silver badges60 bronze badges
asked Mar 3, 2022 at 11:18

1 Answer 1

3

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,
 ),
 ),
 ),
 ),
 ),
 ],
 ),
 ),
 );
 }
 }
answered Mar 3, 2022 at 11:29
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, a complete answer that is what I needed

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.