微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter - 文本字段添加新行

如何解决Flutter - 文本字段添加新行

我尝试使用 keyboardType: TextInputType.visiblePassword 创建一个 TextField 以禁用预测文本,但我无法换行,因为它是一个提交按钮。我尝试添加 textInputAction: TextInputAction.newline 但也不起作用。

我的文本字段:

TextField(
     focusNode: myFocusNode,autocorrect: false,enableSuggestions: false,toolbarOptions: ToolbarOptions(copy: false,cut: false,paste: false),keyboardType: TextInputType.visiblePassword,textInputAction: TextInputAction.newline,autofocus: true,maxLines: null,controller: textEditor,decoration: Inputdecoration(fillColor: Colors.grey[100])
))));

解决方法

将 TextInputAction.newline 更改为 TextInputAction.next

TextField(
     focusNode: myFocusNode,autocorrect: false,enableSuggestions: false,toolbarOptions: ToolbarOptions(copy: false,cut: false,paste: false),keyboardType: TextInputType.visiblePassword,textInputAction: TextInputAction.newline,autofocus: true,maxLines: null,controller: textEditor,decoration: InputDecoration(fillColor: Colors.grey[100])
))));
,

您只需添加以下参数即可禁用预测文本。

enableSuggestions: false,

但是要启用多行,您需要将“keyboardType”更改为“TextInputType.multiline”。

TextField(
        autocorrect: false,keyboardType: TextInputType.multiline,decoration: InputDecoration(fillColor: Colors.grey[100]))
,

我在 RawKeyboardListener 中添加了我的文本字段,当用户按“Enter”时,它会自动检测。

RawKeyboardListener(
      focusNode: FocusNode(),onKey: (event) {
             if(event.isKeyPressed(LogicalKeyboardKey.enter)) {
                 int cursorPos = textEditor.selection.base.offset;
                 textEditor.text = textDebut + '\n' + textFin;
                 textEditor.selection = TextSelection.fromPosition(TextPosition(offset: cursorPos + 1));}},child:TextField(
      focusNode: myFocusNode,decoration: InputDecoration(fillColor: Colors.grey[100]),onChanged: (String text) {print(text);}))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。