flutter - Flutter:ShowDialog 不能与 ListTile 的 OnTap() 方法一起使用
问题描述
我正在使用一个抽屉来创建一个包含选项的 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);
},
),
这是一个演示:
其他 ListTiles 在其 onTap() 方法中只有 Navigator.pop()。解决方案
该对话框未显示,因为您立即使用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);
},
),
],
),
),
输出:
推荐阅读
- reactjs - 呈现非 React 组件的组件上的命令式逻辑(大部分)
- git - 子树存储库和主存储库都包含相同的文件。这是正常的 git 行为吗?
- html - Bootstrap 使用另一列的空间
- python - Matplotlib 集合未清除
- python - 将连续变量下采样到均匀分布
- typescript - 是否可以创建一个导出 mixins 的 Typescript 模块?
- discord.py - How could I ping @here in Discord.py?
- postgresql - 加速一个简单的 postgres 查询
- php - 在 laravel 形式中,url 有效,但路由无效
- java - 无法正常运行我的(自己的)Minecraft Mod