Reputation: 1075
I have a TextFormField
in a Flutter app and I don't want it to lose focus when the user presses enter, which is the default behavior. I've tried a couple of things involving giving the field a FocusNode
:
FocusNode
and onKeyEvent
(does not work)In this approach, the conditional was never true.
In initState
:
_focusNode = FocusNode(onKeyEvent: (node, event) {
if (event.logicalKey == LogicalKeyboardKey.enter) {
return KeyEventResult.handled;
}
return KeyEventResult.ignore;
});
FocusNode
and requestFocus
(works)This approach works but feels hacky. I'm wondering if there is a simpler or more straightforward way.
In initState
:
_focusNode = FocusNode();
In build
:
TextFormField(
focusNode: _focusNode,
onFieldSubmitted: (_) => _focusNode.requestFocus(),
);
Upvotes: 3
Views: 1891
Reputation: 56
The simple solution to this problem is to add keyboardType
parameter and pass TextInputType.multiline
.
TextField(
keyboardType: TextInputType.multiline,
...
)
Upvotes: 0
Reputation: 2900
You need to set the textInputAction
parameter,
TextField(
textInputAction: TextInputAction.none,
...
)
Upvotes: 6