flutter TextField 不居中问题,亲自测试有效

文章描述了在使用Flutter的TextField组件时遇到的问题,当输入内容超过一行,字体显示不完整。此外,添加contentPadding:EdgeInsets.zero属性后,在不同平板设备上存在适配差异,表现为高度不一致。解决方案是通过调整InputDecoration的各种边框设置为透明,并提供了代码示例来修复这些问题。

现象:

使用TextField之后,输入超过一行,字体会显示只有上半截;

加了这个属性之后contentPadding: EdgeInsets.zero,

不同平板适配有差异,有些高度正常,有些偏低。

解决方案:

重点看红色字体部分。

TextField(
  //是否默认获得焦点
  // autofocus: true,
  controller: editingController,
  onTap: () {},
  decoration: InputDecoration(
    focusedBorder: const OutlineInputBorder(
        borderSide:
        BorderSide(width: 0, color: Colors.transparent)),
    disabledBorder: const OutlineInputBorder(
        borderSide:
        BorderSide(width: 0, color: Colors.transparent)),
    enabledBorder: const OutlineInputBorder(
        borderSide:
        BorderSide(width: 0, color: Colors.transparent)),
    border: const OutlineInputBorder(
        borderSide:
        BorderSide(width: 0, color: Colors.transparent)),
    contentPadding: EdgeInsets.zero,

    hintText: "搜索",
    hintStyle: TextStyle(
      color: const Color(0xFF336699),
      fontSize: 14,
    ),
//输入框前面的图片
    prefixIcon: Image.asset(Assets.search),
//输入框后面的图片,如果输入框有内容,则显示清除按钮,否则不显示
    suffixIcon: (GlobalVar().searchKey.isNotEmpty && FocusScope.of(context).hasFocus)?GestureDetector(
      child: Image.asset(Assets.close),
      onTap: () {
        editingController.clear();
        GlobalVar.searchKey = "";
        UIUtil.unfocused(context);
      },
    ):null,
  ),
)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值