Search code examples
flutterdartflutter-alertdialog

How to show alertdialog when button is pressed?


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'),
    );
  }
}

Solution

  • You have to call showDialog function

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