0

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'),
    );
  }
}
Aadarsh Patel
  • 167
  • 4
  • 14

3 Answers3

9

You have to call showDialog function

AppBar(
  title: Text('Lorem Ipsum'),
  leading: IconButton(
    icon: Icon(Icons.info),
    onPressed: () => showDialog(
      context: context,
      builder: (context) => AboutWidget(),
    ),
  ),
)
Crazy Lazy Cat
  • 13,595
  • 4
  • 30
  • 54
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,405
  • 6
  • 28
  • 69
Steve
  • 914
  • 8
  • 17
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,405
  • 6
  • 28
  • 69
Parth Vora
  • 25
  • 7