【问题标题】:How to adjust Flutter TextField height?如何调整 Flutter TextField 的高度?
【发布时间】:2020-06-12 03:19:40
【问题描述】:

我正在使用 Flutter 构建一个社交网络应用程序,并且所有的 TextField 都非常高。我曾尝试调整contentPadding 参数,但它不起作用。当我删除inputDecoration(即将其设置为null)时问题就消失了,但在这种情况下我无法显示任何提示文本。

我还尝试将 TextField 包装在 Container 中并设置 Container 的高度,但这也无济于事。它只会扭曲整个 TextField。

【问题讨论】:

    标签: android ios flutter mobile


    【解决方案1】:

    使用这两行来控制TextFormField 内部InputDecoration 的高度。

    isDense: true, 
    contentPadding: EdgeInsets.fromLTRB(10, 10, 10, 0),
    

    完整示例

    Material(
                    elevation: 4,
                    shadowColor: Colors.blue,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                    child: Padding(
                      padding: const EdgeInsets.only(left: 12),
                      child: TextFormField(
                        controller: searchProvider.searchController,
                        keyboardType: TextInputType.text,
                        decoration: InputDecoration(
                            hintText: 'hintText',
                            isDense: true, // important line
                            contentPadding: EdgeInsets.fromLTRB(10, 10, 10, 0),// control your hints text size
                            hintStyle: TextStyle(letterSpacing: 2, color: Colors.black54, fontWeight: FontWeight.bold),
                            fillColor:  Colors.white30 ,
                            filled: true,
                            border: OutlineInputBorder(borderRadius: BorderRadius.circular(30), borderSide: BorderSide.none)),
                      ),
                    ),
                  ),
    

    【讨论】:

    • 这个!我将TextFormField 放入Container 并更改了contentPadding。像魅力一样工作。谢谢!
    【解决方案2】:

    试试这个!这会稍微减少!

                   TextField(
                          decoration: InputDecoration(
                            contentPadding:EdgeInsets.fromLTRB(10,0,10,0),
                            //The above line will help !
                            border: OutlineInputBorder(),
                            labelText: 'Name',
                            labelStyle:
                                TextStyle(color: Colors.white, fontSize: 17),
                          ),
                          style: TextStyle(color: Colors.white, fontSize: 17),
                        ),
    

    【讨论】:

    • 这似乎是 Flutter 安装的问题,因为在我的其他应用程序中,TextFields 仍然是正常大小。我一直在寻找更强大的解决方案,但我会试一试
    【解决方案3】:

    试试这个:

    TextField(
        decoration: InputDecoration(
            isDense: true,
            contentPadding: EdgeInsets.zero,
            border: InputBorder.none,
            hintText: Strings.settingGoodsTitle
        )
    )
    

    【讨论】:

      猜你喜欢
      • 2019-10-28
      • 2021-06-14
      • 2023-03-10
      • 1970-01-01
      • 2019-12-27
      • 2021-10-28
      • 2018-08-04
      • 1970-01-01
      • 2018-12-07
      相关资源
      最近更新 更多