flutter - 无法从抽屉中关闭 alertDialog
问题描述
我正在尝试单击showDialog
元素时drawer
:
ListTile(
title: Text('Home'),
leading: Icon(Icons.home_outlined),
onTap: () {
Navigator.pop(context);
showDialog(
context: context,
child: AlertDialog(
title: Text('Confirm Sign out'),
content: Text('Are sure to sign out from app now?'),
actions: [
FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Cancel'),
),
],
));
},
),
小部件的完整代码drawer
:
class HomeDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
// your widgets goes here
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
title: Text('Notifications'),
leading: Icon(Icons.notifications_outlined),
onTap: () {},
selected: true,
),
Divider(),
ListTile(
title: Text('Logout'),
leading: Icon(Icons.logout),
onTap: () {
Navigator.pop(context);
showDialog(
context: context,
child: AlertDialog(
title: Text('Confirm Sign out'),
content: Text('Are sure to sign out from app now?'),
actions: [
FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Cancel'),
),
FlatButton(
onPressed: () async {
await FirebaseAuth.instance.signOut();
},
child: Text('Yes and Confirm'),
)
],
));
},
), //here is a divider
Spacer(),
],
));
}
}
但是,dilaog
并没有解雇,它显示以下错误:
Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
When the exception was thrown, this was the stack:
#0 Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3906:9)
#1 Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3920:6)
#2 Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:4039:12)
#3 Navigator.of (package:flutter/src/widgets/navigator.dart:2218:40)
#4 Navigator.pop (package:flutter/src/widgets/navigator.dart:2107:15)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#016ce
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(140.5, 462.5)
finalLocalPosition: Offset(9.5, 34.0)
button: 1
sent tap down
解决方案
Navigator.pop(context)
从中删除onTap
,
为什么它不起作用?
因为这个当前小部件会从小部件树中弹出,所以在该上下文之后,当前小部件也将不可用。
onTap: () {
Navigator.pop(context); // Remove this to pop from here
// Rest code
}
推荐阅读
- p5.js - 如何为该图像设置动画以与 P5.js 中的 BPM 匹配?
- r - 我如何让我的箱线图显示两列而不是一列
- c - GetQueuedCompletionStatus - 如何在没有任何套接字结构的情况下读取客户端消息
- artifactory - 如何让(远程/虚拟)存储库实际显示在 Artifactory OSS 的存储库管理页面中?
- json - 如何使用 Perl 从文件中提取某些数据?
- python - AWS CDK Python - 将变量从 config.py 传递到 lambda.funtion 环境选项
- postman - Mimik 智能汽车用例——Postman 模拟
- typescript - 如何告诉 TypeScript 一个对象只能从数组中的值返回对象属性?
- sql - Markdown、html 和 sql 兼容性
- java - HttpsStatusCodeException 返回 null:Spring Boot