0

How can I create a AlertDialog by clicking/tapping on ListTile. Currently I'm doing this and nothing happens when it's clicked.

body: ListView(
        children: <Widget>[
          ListTile(
            title: Text('Theme'),
            onTap: (){
              AlertDialog(
                title: Text('Hi'),
              );
            },
          )
        ],
      ),

PS: I'm a noob, please go easy on me.

codekls
  • 149
  • 4
  • 16

2 Answers2

3

you are very close, you created the dialog, just need to show it:

body: ListView(
        children: <Widget>[
          ListTile(
            title: Text('Theme'),
            onTap: () {
              AlertDialog alert = AlertDialog(
                title: Text('Hi'),
              );
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return alert;
                },
              );
            },
          )
        ],
      ),
Zvi Karp
  • 3,621
  • 3
  • 25
  • 40
1

Change your ListTile with this.

ListTile(
  title: Text('Theme'),
  onTap: () {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('Alert Dialog Example'),
            content: Text('Alert Dialog Body Goes Here  ..'),
            actions: <Widget>[
              FlatButton(
                  onPressed: () => Navigator.of(context).pop(),
                  child: Text('OK')),
            ],
          );
        });
  },
)

I have also added some properties to use the AlertDialog(), like title, content and actions

encubos
  • 2,915
  • 10
  • 19