简体   繁体   English

Flutter:ShowDialog 不适用于 ListTile 的 OnTap() 方法

[英]Flutter: ShowDialog not working with the OnTap() method of a ListTile

I am using a drawer to create a menu that houses ListTiles for the options.我正在使用一个抽屉来创建一个包含选项的 ListTiles 的菜单。 I want to create a popup when the tiles are tapped by the user.我想在用户点击瓷砖时创建一个弹出窗口。 Currently, the code displays nothing when the tiles are tapped even though after the showDialog there is a Navigator.pop().目前,即使在 showDialog 之后有一个 Navigator.pop(),当点击图块时代码也不显示任何内容。

// Drawer that includes the ListTiles in question
 Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[
              DrawerHeader(
                child: Text('Smash Tracker'),
                ),
              ),
              ListTile(
                title: Text(
                    'About',
                  style: TextStyle(
                    fontFamily: 'Smash',
                    fontSize: 15.0,
                    color: Color.fromRGBO(77, 114, 152, 1.0),
                  ),
                ),
                onTap: () {
                  // Show PopUp
                  showDialog(context: context, child:
                    new AlertDialog(
                      title: new Text(
                        'About',
                        style: TextStyle(fontFamily: "Smash"),
                      ),
                      content: new Text(
                        'This is a placeholder. This is a placeholder. This is a placeholder. This is a placeholder.',
                        style: TextStyle(fontFamily: "Smash"),
                      ),
                    )
                  );

                  // Doesn't run
                  Navigator.pop(context);
                },
              ),

Here is a demo:这是一个演示:

via GIPHY通过 GIPHY

The other ListTiles have the only have Navigator.pop() inside of their onTap() method. 其他 ListTiles 在其 onTap() 方法中只有 Navigator.pop()。

The dialog isn't showing because you are popping it immediately with the Navigator.pop(context) .该对话框未显示,因为您正在使用Navigator.pop(context)立即弹出它。 You can await the Dialog since it returns a Future<T> before popping.您可以await Dialog ,因为它在弹出之前返回Future<T>

I added a demo using your widget tree as an example:我以您的小部件树为例添加了一个演示:

Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
              child: Text('Smash Tracker'),
            ),
            ListTile(
              title: Text(
                'About',
                style: TextStyle(
                  fontFamily: 'Smash',
                  fontSize: 15.0,
                  color: Color.fromRGBO(77, 114, 152, 1.0),
                ),
              ),
              onTap: () async { // mark the function as async
                print('tap');
                // Show PopUp

                // await the dialog
                 await showDialog(
                    context: context,
                    child: new AlertDialog(
                      title: new Text(
                        'About',
                        style: TextStyle(fontFamily: "Smash"),
                      ),
                      content: new Text(
                        'This is a placeholder. This is a placeholder. This is a placeholder. This is a placeholder.',
                        style: TextStyle(fontFamily: "Smash"),
                      ),
                    ));

                // Doesn't run
                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),

OUTPUT: OUTPUT:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM