Flutter开发Fire TV/Stick应用时按遥控器返回键无法正确关闭弹窗问题
问题修复方案
核心原因
- 关闭弹窗时使用了父页面上下文配合
rootNavigator: true操作,直接触发了根路由的页面回退 - 弹窗未拦截系统返回事件,Fire TV遥控器的返回键事件直接透传给了父页面处理
完整修复代码
修改弹窗显示逻辑,用WillPopScope包裹AlertDialog主动拦截处理返回事件:
_showInfoModal(context) { return showDialog( context: context, // 可根据需求配置是否允许点击弹窗外部关闭 barrierDismissible: true, builder: (BuildContext dialogContext) { return WillPopScope( onWillPop: () async { // 仅关闭当前弹窗,使用弹窗自身的context Navigator.pop(dialogContext); // 标记事件已处理,不再向上传递给父页面 return false; }, child: AlertDialog( backgroundColor: Colors.transparent, content: Container( child: Center( child: Text(widget.message, style: TextStyle(color: Colors.white)), ), ) ), ); }, ); }
额外适配说明
删除原有的全局Navigator.of(context, rootNavigator: true).pop()逻辑,弹窗内部已经完成返回键处理。如果你的应用全局监听了遥控器按键事件,需要新增弹窗显示状态判断,弹窗显示时不触发父页面的返回逻辑即可。
内容的提问来源于stack exchange,提问作者Suhas D S
相关产品推荐
相关产品推荐

