如何获取 Flutter AlertDialog 操作来监视键盘事件(输入键)?

Era*_*ore 1 keyboard dart 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();
              },
            ),
          ],
        );
      },
    );
  }
Run Code Online (Sandbox Code Playgroud)

有没有办法默认键盘上的“输入”键就像按下删除键一样?也许还不是最安全的 UI 体验,但主要只是要求了解这里的基础。

谢谢,贾斯汀

小智 5

您唯一需要做的就是将 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 ...
Run Code Online (Sandbox Code Playgroud)