允许用户在单个 TextFormfield 中继续输入

问题描述

我遇到了问题。当用户输入标题时,直到字符的长度小于 textformfield 的长度(参见屏幕截图 1)时,才可以。但是,如果字符的长度超过了 textformfield 的长度,则 textformfield 似乎变成了 2 行。我想保留单行,但允许用户继续打字而不会出现问题。我该怎么做?

谢谢,

                          Container(
                            height: 45,child: Row(
                              children: [
                                Expanded(
                                  child: SizedBox(
                                    child: TextFormField(
                                      controller: _titleTextField,focusNode: _titleTextFieldFocusNode,keyboardType: TextInputType.text,textInputAction: TextInputAction.next,maxLines: 1,maxLength: null,decoration: Inputdecoration(
                                        labelText: 'Title',border: OutlineInputBorder(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(3)),),flex: 1,)
                              ],)),

Typed as the length of the formtextfield

Typed more 1 character than the length of the formtextfield

解决方法

如果你想保留一行 TextFormField,你可以调整字段内部的填充,问题是即使你告诉 flutter 你只想要一行,文本也在试图向上移动。

做一些这样的事情来调整填充并保持居中

decoration: InputDecoration(
       contentPadding:
              const EdgeInsets.only(
              bottom: 5.0,top: 5.0)),labelText: 'Title',border: OutlineInputBorder(
           borderRadius: BorderRadius.all(
               Radius.circular(3)),),