flutter - 如何获取 Flutter AlertDialog 动作以监视键盘事件(输入键)?
问题描述
有没有办法让颤动中的“输入”键键盘事件默认为我的 AlertDialog 上的一个操作按钮?注意我指的是在有物理键盘的情况下使用 web 的颤振。例如我有这个对话框:
Future<void> _confirmDelete(int index) async {
var fp = SessionInfo.of(context).myFloorplans.entries.toList()[index].value;
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Confirm Delete?'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8.0))),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text("Please confirm deleting ${fp.name}"),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text('Delete'),
onPressed: () {
_deleteFloorplan(index);
Navigator.of(context).pop();
},
),
],
);
},
);
}
有没有办法默认键盘上的“输入”键就像他们点击删除一样?也许还不是最安全的 UI 体验,但主要只是要求了解这里的基础。
谢谢,贾斯汀
解决方案
您唯一需要做的就是将 AlertDialog 包装在 RawKeyboardListener 中。检查这个例子:
RawKeyboardListener(
// its better to initialize and dispose of the focus node only for this alert dialog
focusNode: FocusNode(),
autofocus: true,
onKey: (v) {
if (v.logicalKey == LogicalKeyboardKey.enter) {
_deleteFloorplan(index);
Navigator.pop(context);
}
},
child: your alert dialog ...
推荐阅读
- java - 使用 Java 8 Streams 映射、聚合和组合总计
- java - 我将如何处理我的代码中的多个窗口?
- r - R:正则表达式 \\s 不是空格
- .net-core - 是否支持在 NET Core 中为 Microsoft Teams 创建扩展?
- tensorflow - TensorFlow中的三角矩阵矩阵乘以`trmm`
- java - 为什么我们在 java-9 模块系统中需要静态?
- corda - Corda - 自 Corda 4 升级以来测试接受器流失败
- javascript - 车把和 js:在 for 循环内分配不同的 id 值并在循环外引用它们
- sql - 返回跨不同组的时间增量的中值
- elasticsearch - 结合查询与日期直方图