首页 > 解决方案 > 如何仅将填充应用于 Flutter 中 TextField 中的文本?

问题描述

没有填充我得到这个结果:

没有填充

像这样

Padding(padding: EdgeInsets.all(20.0), child: TextField())

我得到以下结果:

带填充物

可能有点难看,但你只需要看看边缘的红色徽章就能明白我的意思。

我只想用我的填充移动文本,但实际上整个TextField都应用了填充,即下划线随着填充移动,但我希望下划线仍然穿过整个视图,只有文本内部TextField受填充影响。

标签: dartflutter

解决方案


将填充应用于TextField. 您可以申请

TextField 的装饰属性中的 ItemDecoration 的 contentPadding 属性。

像这样:

TextField(
  textAlign: TextAlign.left,
  decoration: InputDecoration(
    hintText: 'Enter Something',
    contentPadding: EdgeInsets.all(20.0),
  ),
)

推荐阅读