Flutter:如何拥有一个多行 TextFormField,当我按 Enter 键时不生成新行,而是执行一个函数?

发布于 2025-01-20 18:25:28 字数 628 浏览 1 评论 0原文

我有一个TextFormField,在我按Enter时会执行功能,以便额外的方便(除了按下按钮时执行该功能)。

当我使TextFormField能够具有多行(因此高度增长)时,Enter键不会导致函数执行,但它只是创建了一条新行。

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,
)

“回答您自己的问题” - >在下面查看我解决此问题的解决方案

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2025-01-27 18:25:28

我要做的工作要做的就是将最后一行更改为:keyboardType:textinputtype.text
现在,如果文本变为大,我的TextFormField仍将跳入新线路,但是如果我按Enter,则执行我的功能。

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,
)

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,
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文