如何改变TextField的高度和宽度?

How to change TextField's height and width?

如何自定义 TextFieldwidthheight

要调整宽度,您可以用 Container 小部件包裹 TextField,如下所示:

Container(              
  width: 100.0,
  child: TextField()
)

关于 TextField 的高度,我不太确定你想要什么,但你绝对可以看看 TextStyle 小部件,你可以用它来操作fontSize and/or height

Container(              
  width: 100.0,
  child: TextField(                                 
    style: TextStyle(
      fontSize: 40.0,
      height: 2.0,
      color: Colors.black                  
    )
  )
)

请记住,TextStyle 中的 height 是字体大小的倍数,根据对 属性 本身的评论:

The height of this text span, as a multiple of the font size.

When [height] is null or omitted, the line height will be determined by the font's metrics directly, which may differ from the fontSize. When [height] is non-null, the line height of the span of text will be a multiple of [fontSize] and be exactly fontSize * height logical pixels tall.

最后但同样重要的是,你可能想看看decoration属性的你TextField,这给了你很多可能性

编辑: 如何更改 TextField

的内部 padding/margin

您可以使用 InputDecorationTextFielddecoration 属性。例如,您可以这样做:

TextField(                                
    decoration: const InputDecoration(
        contentPadding: const EdgeInsets.symmetric(vertical: 40.0),
    )
)

您可以在Container中尝试margin属性。将 TextField 包裹在 Container 内并调整 margin 属性.

new Container(
  margin: const EdgeInsets.only(right: 10, left: 10),
  child: new TextField( 
    decoration: new InputDecoration(
      hintText: 'username',
      icon: new Icon(Icons.person)),
  )
),

截图:


Widget _buildTextField() {
  final maxLines = 5;

  return Container(
    margin: EdgeInsets.all(12),
    height: maxLines * 24.0,
    child: TextField(
      maxLines: maxLines,
      decoration: InputDecoration(
        hintText: "Enter a message",
        fillColor: Colors.grey[300],
        filled: true,
      ),
    ),
  );
}

我想你想更改 TextField 内部 padding/margin

您可以通过添加 isDense: truecontentPadding: EdgeInsets.all(8) 属性来实现,如下所示:

Container(
  padding: EdgeInsets.all(12),
  child: Column(
    children: <Widget>[
      TextField(
        decoration: InputDecoration(
          border: OutlineInputBorder(),
          labelText: 'Default TextField',
        ),
      ),
      SizedBox(height: 16,),
      TextField(
        decoration: InputDecoration(
          border: OutlineInputBorder(),
          labelText: 'Densed TextField',
          isDense: true,                      // Added this
        ),
      ),
      SizedBox(height: 16,),
      TextField(
        decoration: InputDecoration(
          border: OutlineInputBorder(),
          labelText: 'Even Densed TextFiled',
          isDense: true,                      // Added this
          contentPadding: EdgeInsets.all(8),  // Added this
        ),
      )
    ],
  ),
)

将显示为:

文本字段(最小行数:1,最大行数:5,最大长度强制:真)

如果您使用“suffixIcon”折叠 TextField 的高度,请添加: 后缀图标约束

TextField(
                    style: TextStyle(fontSize: r * 1.8, color: Colors.black87),
                    decoration: InputDecoration(
                      isDense: true,
                      contentPadding: EdgeInsets.symmetric(vertical: r * 1.6, horizontal: r * 1.6),
                      suffixIcon: Icon(Icons.search, color: Colors.black54),
                      suffixIconConstraints: BoxConstraints(minWidth: 32, minHeight: 32),
                    ),
                  )

要增加 TextField 小部件的高度,只需使用小部件附带的 maxLines: 属性。例如: 文本域( 最大行数:5 ) // 它将增加文本字段的高度和宽度。

您可以简单地将文本字段小部件包装在填充小部件中..... 像这样,

Padding(
         padding: EdgeInsets.only(left: 5.0, right: 5.0),
          child: TextField(
            cursorColor: Colors.blue,

            decoration: InputDecoration(
                labelText: 'Email',
                hintText: 'xyz@gmail.com',
                //labelStyle: textStyle,
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(5),
                  borderSide: BorderSide(width: 2, color: Colors.blue,)),
              focusedBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(10),
                  borderSide: BorderSide(width: 2, color: Colors.green)),
                )

            ),
          ),

使用contentPadding,它会降低文本框或下拉列表的高度

InputDecorator(
                  decoration: InputDecoration(
                      errorStyle: TextStyle(
                          color: Colors.redAccent, fontSize: 16.0),
                      hintText: 'Please select expense',
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(1.0),
                      ),
                      contentPadding: EdgeInsets.all(8)),//Add this edge option
                  child: DropdownButton(
                    isExpanded: true,
                    isDense: true,
                    itemHeight: 50.0,

                    hint: Text(
                        'Please choose a location'), // Not necessary for Option 1
                    value: _selectedLocation,
                    onChanged: (newValue) {
                      setState(() {
                        _selectedLocation = newValue;
                      });
                    },
                    items: citys.map((location) {
                      return DropdownMenuItem(
                        child: new Text(location.name),
                        value: location.id,
                      );
                    }).toList(),
                  ),
                ),
int numLines = 0;

Container(
     height: numLines < 7 ? 148 * WidgetsConstant.height: numLines * WidgetsConstant.height * 24,
     child: TextFormField(
              controller: _bodyText,
              maxLines: numLines < 7 ? 148 : numLines,
              keyboardType: TextInputType.multiline,
              textInputAction: TextInputAction.newline,        
              onChanged: (String value) {
                setState(() {
                  numLines = '\n'.allMatches(value).length + 1;
                });
              },
          ),
     ),

TextField 包裹在 SizedBox 中以获得宽度

SizedBox(
  height: 40,
  width: 150,
  child: TextField(),
)

如果您想在 TextFormField 动态地 中增加高度,同时在其中键入文本。将 maxLines 设置为 null。喜欢

TextFormField(
          onSaved: (newText) {
            enteredTextEmail = newText;
          },

          obscureText: false,
          keyboardType: TextInputType.emailAddress,
          validator: validateName,
          maxLines: null,
          // style: style,
          decoration: InputDecoration(
              contentPadding: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0),
              hintText: "Enter Question",
              labelText: "Enter Question",
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(10.0))),
        ),

这个答案有效,但是当输入字段处于错误状态时会发生冲突,为了更好的方法和更好的控制,你可以使用这个。

InputDecoration(
    isCollapsed: true,
    contentPadding: EdgeInsets.all(9),
)

只需将 TextField() 包裹在 SizedBox() 中并给出大小框的高度

摆脱填充的完美方法是使用InputDecoration.collapsed

它用 Container 包装 GestureDetector 并根据需要用尽可能多的填充、边框和装饰装饰容器。

GestureDetector(
  onTap: () => _focusNode.requestFocus(),
  child: Container(
    padding: const EdgeInsets.all(10),
    decoration: BoxDecoration(
      color: Colors.grey,
      borderRadius: BorderRadius.circular(4),
    ),
    child: TextField(
      focusNode: _focusNode,
      decoration: const InputDecoration.collapsed(
        hintText: 'Search...',
        border: OutlineInputBorder(
          borderSide: BorderSide(
            width: 0,
            style: BorderStyle.none,
          ),
        ),
      ),
    ),
  ),
);

要显示图标,请将 Container 子项更改为 Row,并使用 Icon 和用 Expanded 包裹的 TextField

您可以更改最大值

minLines: 4,
maxLines: 6,

设置 minLines: nullmaxLines: nullexpands:true 让 TextField 填充其父控件的高度:

Container(
  child: TextField(
    minLines: null,
    maxLines: null,
    expands: true
  )
)

请参阅这些文档以获得相同的信息:https://api.flutter.dev/flutter/material/TextField/expands.html

对于宽度,您可以这样做:

Container(
  width: 100,
  child: TextField(
    
  )
)

让 TextField 填充其父窗口小部件的宽度(在本例中为 100 像素)

TextField(
  decoration: InputDecoration(
    border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
    constraints: BoxConstraints.tightFor(width: 327, height: 60),
  ),
);