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

问题描述 投票:0回答:1

我创建了具有自定义高度和不同背景色的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

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

谢谢!

flutter flutter-layout
1个回答
2
投票

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

TextField(
  textAlign: TextAlign.center
  decoration: InputDecoration(
    hintText: ...,
    hintStyle: ...,
    border: ...,
    // contentPadding:
  )
)
© www.soinside.com 2019 - 2024. All rights reserved.