我有一个带有TextFormField的对话框。我想在网络上处理用户,请按Enter接受该字段中的值。 onFieldSubmitted()方法适用于此,但实际上效果很好。如果他们甚至将注意力从场上移开,它就会触发。我真的只想为Enter键触发。我是否必须为此使用RawKeyboardListener()?还是有某种方法可以知道它不是Enter键,而是触发onFieldSubmitted()的焦点更改?这是我的对话框示例代码片段。 CustomTextField只是我将TextFormField扩展为每次都有某种外观。
var res = await showDialog<String>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Row(
children: <Widget>[
Text('Add '),
Container(
width: 8,
),
DropdownButton(
value: _currentAddType,
items: _addTypeDropDownMenuItems,
onChanged: (value) => _changeAddType(value),
),
],
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8.0))),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
CustomTextField(
controller: _roomNameController,
textCapitalization: TextCapitalization.words,
autoFocus: true,
icon: Icon(Icons.text_fields),
hint: "Room Name",
onFieldSubmitted: (value) => { // <======== this is the problem, it works even if just losing focus
setState(() {
Navigator.of(context).pop('Add');
})
},
),
Container(
height: 12,
),
Text(
'Select Gateway:',
style: Theme.of(context).textTheme.headline6,
),
Container(
height: 4,
),
DropdownButton(
value: _currentGateway,
items: _gatewayDropDownMenuItems,
onChanged: (String gateway) {
_currentGateway = gateway;
setState(() {
Navigator.of(context)
.pop();
});
_addGatewayOrRoom();
},
),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Cancel'),
onPressed: () {
setState(() {
Navigator.of(context).pop('Cancel');
});
},
),
Container(
width: 20,
),
FlatButton(
child: Text('Add'),
onPressed: () {
setState(() {
Navigator.of(context).pop('Add');
});
},
),
],
);
},
);
if (res == 'Add') {
await doAddRoom();
_doRefresh();
}