今天遇到了一个关于TextField的问题:
如果给textField设定的布局高度小于它的默认高度,那么它的居中就会有问题.

修改后的效果:
在这里插入图片描述
代码:

Container(
              color: Colors.black12,
              constraints: BoxConstraints(maxHeight: 30),
              child: TextField(
                textAlignVertical: TextAlignVertical.bottom,
                focusNode: secondTextFieldNode,
                decoration: InputDecoration(
                  hintText: '请输入',
                  border: OutlineInputBorder(borderSide: BorderSide.none),
                  //    contentPadding: EdgeInsets.all(0),
                ),
                textCapitalization: TextCapitalization.characters,
              ),
            ),

主要在于

  1. border 不能直接使用InputBorder.none
  2. 如果文字偏上用TextAlignVertical.bottom修正
  3. 旧版本的flutter可能需要设置 contentPadding: EdgeInsets.all(0),

也可以使用isCollapsed: true,来修正:

TextField(
	decoration: InputDecoration(
               		isCollapsed: true,
                	...
                )
)
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐