2

Following is the part of my homepage.dart which is running fine but on click of IconButton nothing happens.

 ...
 return Scaffold(
 appBar: AppBar(
 title: Text('Lorem Ipsum'),
 leading: IconButton( 
 icon: Icon(Icons.info),
 onPressed: () => AboutWidget(),
 ),
 ),
 body: ...

This is my about_widget.dart file where my AboutWidget is defined. What am i doing wrong?

 import 'package:flutter/material.dart';
class AboutWidget extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
 return AlertDialog(
 title: Text('data'),
 );
 }
}
asked Dec 20, 2019 at 10:37

4 Answers 4

8

You have to call showDialog function

AppBar(
 title: Text('Lorem Ipsum'),
 leading: IconButton(
 icon: Icon(Icons.info),
 onPressed: () => showDialog(
 context: context,
 builder: (context) => AboutWidget(),
 ),
 ),
)
answered Dec 20, 2019 at 10:43
Sign up to request clarification or add additional context in comments.

Comments

3

Use Flutter native showDialog function to show a dialog.

For your code, you could try this:

return Scaffold(
 appBar: AppBar(
 title: Text('Lorem Ipsum'),
 leading: IconButton( 
 icon: Icon(Icons.info),
 onPressed: () => showDialog(
 context: context,
 builder: (context){
 return AboutWidget();
 }
 ),
 ),
 ),
);

So when the button is pressed, you should call the showDialog method.

Nimantha
6,5276 gold badges32 silver badges78 bronze badges
answered Dec 20, 2019 at 10:45

Comments

1

Using some button, that also receives a reference to a function:

FilledButton.icon(
onPressed: () => showDialog(
 context: context,
 builder: (context) => CustomDialogAlert(
 title: '¿Desea proceder?',
 description:
 'Esta acción de BORRADO no puede ser deshecha.',
 buttonLeft: 'Cancelar',
 buttonRight: 'Aceptar',
 callbackFunction: _submitFormDelete)),
icon: const Icon(Icons.delete_outline_outlined),
label: const Text('Borrar')
), // FilledButton.icon

And a stateless class:

import 'package:flutter/material.dart';
class CustomDialogAlert extends StatelessWidget {
 final String title;
 final String description;
 final String buttonLeft;
 final String buttonRight;
 final Function() callbackFunction;
 const CustomDialogAlert(
 {super.key,
 this.buttonLeft = 'Cancelar',
 this.buttonRight = 'Ok',
 required this.description,
 required this.title,
 required this.callbackFunction});
 @override
 Widget build(BuildContext context) {
 return AlertDialog(
 title: Text(title),
 content: Text(description),
 actions: <Widget>[
 TextButton(
 onPressed: () => Navigator.pop(context, 'Cancel'),
 child: Text(buttonLeft),
 ),
 TextButton(
 onPressed: () => callbackFunction(),
 child: Text(buttonRight),
 ),
 ],
 );
 }
}

Result is:

enter image description here

answered Nov 6, 2023 at 17:11

Comments

0

On press of the button you are calling AboutWidget(). Its a statless widget. So it needs to be build in the build method of your app. On button click this wont show the dialog. Instead use showDialog method and inside that use your alert dialog to display the dialog on the screen.

Nimantha
6,5276 gold badges32 silver badges78 bronze badges
answered Dec 20, 2019 at 10:44

Comments

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.