Flutter: ShowDialog не работает с методом OnTap () объекта ListTile - PullRequest
0 голосов
/ 02 августа 2020

Я использую выдвижной ящик для создания меню, содержащего ListTiles для опций. Я хочу создать всплывающее окно, когда пользователь касается плиток. В настоящее время код ничего не отображает при касании плиток, хотя после 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);
                },
              ),

Вот демонстрация:

через GIPHY

У других ListTiles есть только Navigator.pop () внутри их метода onTap ().

1 Ответ

1 голос
/ 02 августа 2020

Диалог не отображается, потому что вы сразу открываете его с помощью Navigator.pop(context). Вы можете await Dialog, так как он возвращает Future<T> перед появлением.

Я добавил демонстрацию, используя ваше дерево виджетов в качестве примера:

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

ВЫХОД:

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...