如何让Flutter AlertDialog操作来观察键盘事件(输入键)



有没有办法获得"输入";在flutter中按键键盘事件以默认为我的AlertDialog上的某个操作按钮?注意,我指的是在有物理键盘的网页上使用flutter。例如,我有这样的对话框:

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体验,但主要只是想了解这里的基础。

谢谢,Justin

您唯一需要做的就是将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 ...

我没有看到任何会导致键盘在这个对话框中显示的内容。

无论如何,如果你有一个显示键盘的TextFormField,你可以做一些类似的事情

onFieldSubmitted: (value) {
_deleteFloorplan(index);
Navigator.of(context).pop();
}

每当用户点击文本输入操作按钮(您提到的"输入"键(时,onFieldSubmitted就会被调用

最新更新