【问题标题】:Flutter, content padding for TextFormField not working as expectedFlutter,TextFormField 的内容填充未按预期工作
【发布时间】:2020-03-05 09:04:03
【问题描述】:

Flutter 应用程序,android:我正在尝试减少 TextFormField 的底部填充(我需要在相当小的屏幕上放置相当多的字段 [移动 android 数据收集器])。

我曾尝试使用 contentPadding InputDecoration,但它并没有像我预期的那样工作。

这是底部填充为 10:

decoration: InputDecoration(
...
  contentPadding: new EdgeInsets.fromLTRB(10, 10, 10, 10):

这是底部填充为 4:

decoration: InputDecoration(
...
  contentPadding: new EdgeInsets.fromLTRB(10, 10, 10, 4):

如您所见,TextFormFields 内文本的底部填充没有太大变化,仍然浪费了太多空间;此外,由于某种原因,顶部填充也发生了变化,使得内容文本与标签文本过于接近。我不希望顶部填充发生变化。

如何减少底部填充(文本和边框之间)?

编辑 2019 年 11 月 18 日,每个请求的完整 TextFormField:

TextFormField(
  initialValue: widget.initialValue,
  textInputAction: TextInputAction.next,
  enabled: widget.enabled,
  minLines: widget.lines,
  maxLines: widget.lines,
  autofocus: widget.autoFocus,
  focusNode: widget.focusNode,
  keyboardType: widget.keyboardType,
  maxLength: widget.maxLength,
  maxLengthEnforced: true,
  readOnly: widget.readonly,
  inputFormatters: widget.inputFormatters,
  controller: widget.controller,
  style: TextStyle(
    fontSize: 17,
    color: widget.textColor != null ? widget.textColor : null,
  ),
  onFieldSubmitted: widget.onFieldSubmitted,
  onEditingComplete: widget.onEditingComplete,
  decoration: InputDecoration(
    labelText: widget.labelText,
    labelStyle: TextStyle(color: widget.enabled ? Colors.grey[600] : Colors.grey[500]),
    hintText: widget.hintText != null ? widget.hintText : widget.labelText,
    counterText: '',
    errorText: widget.errorText,
    filled: true,
    fillColor: widget.fillColor != null ? widget.fillColor : widget.enabled ? Colors.white : Colors.grey[100],
    contentPadding: new EdgeInsets.fromLTRB(10, 10, 10, 10),
    border: OutlineInputBorder(),
  ),
),

【问题讨论】:

  • 可以分享代码吗?
  • @AlvinJohnNiravukalayil 完成。
  • 无法删除那些填充,你可以使用InputDecoration.collapsed(hintText: null) 删除所有填充,但是使用这个你不能添加所有其他样式。

标签: android flutter flutter-layout


【解决方案1】:

尝试在 inputDecoration 中添加 isDense:true 以获得小于默认值的填充。这对我有帮助


decoration: InputDecoration(
         isDense: true,


        // contentPadding: new EdgeInsets.fromLTRB(0, 0, 0, 0),

         contentPadding: EdgeInsets.only(top: 4,bottom: 4,left: 6,right: 6),
         hintText: "Enter name",

         hintStyle: TextStyle(color: Colors.grey),
         border: InputBorder.none,
       ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2022-07-10
    • 2021-07-22
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多