Flutter:如何拥有一个多行 TextFormField,当我按 Enter 键时不生成新行,而是执行一个函数?
我有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我要做的工作要做的就是将最后一行更改为:
keyboardType:textinputtype.text
。现在,如果文本变为大,我的TextFormField仍将跳入新线路,但是如果我按Enter,则执行我的功能。
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.