flutter报这个错 KeyEvent ViewRootImpl KeyEvent action=ACTION_UP keyCode=KEYCODE_BACK scanCode=0 metaState=0 flags=0x48 repeatCount=0 eventTime=74545501 downTime=74545469 deviceId=-1 source=0x101 displayI
这个错误是指在Flutter应用程序中按下了“返回”键,但没有处理该事件。您需要使用Flutter的键盘和鼠标事件处理程序来处理此事件并执行相应的操作。您可以尝试在Flutter应用程序中添加以下代码来处理“返回”键事件:
//监听返回键事件
class MyHomePageState extends State<MyHomePage> {
Future<bool> _onWillPop() {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('确认退出应用吗?'),
content: new Text('如果您退出,您将离开应用程序'),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text('否'),
),
new FlatButton(
onPressed: () => Navigator.of(context).pop(true),
child: new Text('是'),
),
],
),
) ?? false;
}
//...
//...
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
//...
),
);
}
//...
//...
}
这将显示一个警告对话框,询问用户是否要退出应用程序,并在用户点击“是”按钮时关闭应用程序。
原文地址: https://www.cveoy.top/t/topic/bE3Y 著作权归作者所有。请勿转载和采集!