更新到Flutter 1.12.13后,TextField提示/输入文本没有居中时没有prefixIcon

时间:2020-01-16 20:46:16

标签: flutter flutter-layout

我创建了具有自定义高度和不同背景色的TextField。这是代码段:

Container(
  width: width,
  height: 35,
  decoration: BoxDecoration(
    color: Constants.greyColor,
    borderRadius: BorderRadius.all(Radius.circular(2)),
  ),
  padding: EdgeInsets.symmetric(horizontal: 15),
  child: TextField(
    textAlign: descriptor == null ? TextAlign.center : TextAlign.left,
    decoration: InputDecoration(
      hintText: placeholder,
      hintStyle: Constants.textStyleTextInputHint,
      border: InputBorder.none,
      contentPadding: EdgeInsets.symmetric(horizontal: 0),
    ),
    onChanged: (value) {
      state.didChange(value);
    },
    obscureText: isPassword,
    style: Constants.textStyleTextInput,
    cursorColor: Constants.primaryColor,
  ),
),

在我最近将Flutter更新到1.12.13 + hotfix.5版本之前,它一直运行良好。现在,提示文本以及输入文本不再居中。似乎Container不再更改TextField的高度。像这样:

TextField with hint text being offset

如果添加prefixIcon,则文本和图标将居中对齐。像这样:

TextField with prefixIcon

有人知道我如何在没有图标的情况下使文本居中吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

我认为这是因为您添加了contentPadding属性,即使它只是水平的。您是否尝试过删除它?

TextField(
  textAlign: TextAlign.center
  decoration: InputDecoration(
    hintText: ...,
    hintStyle: ...,
    border: ...,
    // contentPadding:
  )
)