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);}))