文本字段的文本对齐方式未垂直对齐

我正在尝试构建一个具有自定义文本字段高度的表单,我想使文本字段文本和命中文本垂直居中。这是我的代码

      SizedBox(
             height: 40,
              child:
              TextField(
                style: TextStyle(
                  fontSize: 14,
                ),
                textAlignVertical: TextAlignVertical.center,
                textAlign: TextAlign.left,
                maxLines: 1,
                decoration: InputDecoration(
                    filled: true,
                    fillColor: Color(0xff5a9fd6).withOpacity(0.15),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Color(0xff5a9fd6).withOpacity(1.0),
                      ),
                      borderRadius: BorderRadius.circular(2.0),
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.transparent,
                      ),
                      borderRadius: BorderRadius.circular(1.0),
                    )
                ),
              )
          ),

当我减小字体大小时,文本会垂直对齐,但是我想使用14以上的字体。

enter image description here

评论
  • edolor
    edolor 回复

    You can to adjust contentPadding to center your text vertically, for example:

    TextField(
    decoration: InputDecoration(
        contentPadding: const EdgeInsets.symmetric(vertical: 8),
    )
    
  • 赫连迎蓉
    赫连迎蓉 回复

    You can set the contentPadding of the TextField as 0 or according to your requirement.

    SizedBox(
                 height: 40,
                  child:
                  TextField(
                    style: TextStyle(
                      fontSize: 14,
                    ),
    
                    textAlignVertical: TextAlignVertical.center,
                    textAlign: TextAlign.left,
                    maxLines: 1,
                    decoration: InputDecoration(
                        contentPadding: const EdgeInsets.all(0),
                        filled: true,
                        fillColor: Color(0xff5a9fd6).withOpacity(0.15),
                        focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: Color(0xff5a9fd6).withOpacity(1.0),
                          ),
                          borderRadius: BorderRadius.circular(2.0),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: Colors.transparent,
                          ),
                          borderRadius: BorderRadius.circular(1.0),
                        )
                    ),
                  )
              ),