Cedric
Cedric

Reputation: 980

Flutter: How can I have a multiline TextFormField that doesn't make a new line when I press enter, but executes a function?

I had a TextFormField, that executes a function when I press enter, for extra convenience (In addition to that function being executed when a button is pressed).

When I made the TextFormField able to have multiple lines (so it grows in height), the enter key doesn't cause the function to execute anymore, but it just creates a new line.

TextFormField(
  validator: (String language) {
    if (value.isEmpty) return 'Please provide a translation in $language.';
    return null;
  },
  autovalidateMode: AutovalidateMode.onUserInteraction,
  controller: _mainControllers[ws],
  textInputAction: TextInputAction.go,
  onFieldSubmitted: (_) => _saveMainTranslation(context),
  //these 2 lines are important
  maxLines: null,
  keyboardType: TextInputType.multiline,
)

"Answer your own question" -> see my solution to this problem below

Upvotes: 1

Views: 2311

Answers (1)

Cedric
Cedric

Reputation: 980

All I had to do to make this work was change the last line to: keyboardType: TextInputType.text.
Now my TextFormField will still jump into a new line, if the text becomes to big, but if I press enter, my function is executed.

TextFormField(
  validator: (String language) {
    if (value.isEmpty) return 'Please provide a translation in $language.';
    return null;
  },
  autovalidateMode: AutovalidateMode.onUserInteraction,
  controller: _mainControllers[ws],
  textInputAction: TextInputAction.go,
  onFieldSubmitted: (_) => _saveMainTranslation(context),
  //these 2 lines are important
  maxLines: null,
  keyboardType: TextInputType.text,
)

Upvotes: 5

Related Questions