You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中TextFormField调整字号后内容无法居中问题

问题描述

第一个TextFormField设置字号为16时,内容居中显示正常;但第二个TextFormField(密码输入框,开启了obscureText)设置字号为30时,内容无法居中。

问题代码

自定义TextFormFieldRounded组件核心代码:

TextFormField(
  maxLines: 1,
  scrollPadding: EdgeInsets.zero,
  validator: validator,
  readOnly: readOnly ?? false,
  onChanged: onChanged,
  controller: controller,
  obscureText: obscureText ?? false,
  keyboardType: keyBoardType,
  // cursorHeight:  GeneralHelper.scale(context: context,height: obscureText == true ?30:17),
  style: TextStyle(
    color: Colors.black,
    fontSize: GeneralHelper.scale(
      context: context,
      // height: GeneralHelper.scale(context: context,height:17),
      height: GeneralHelper.scale(context: context,height: obscureText == true ?30:17),
      // height: 17,
    ),
  ),
)

组件使用场景:

TextFormFieldRounded(
  radius: GeneralHelper.scale(context: context, height: 39.5),
  controller: txtPhoneNumber,
  hintStyle: TextStyle(
    color: const Color(0xFF81C59E),
    fontSize: GeneralHelper.scale(context: context, height: 16),
    fontWeight: FontWeight.w500,
  ),
  elevation: 0,
  backgroundColor: const Color(0xFFE3F4E1),
  borderSize: BorderSide(
      width: 0.5,
      style: BorderStyle.solid,
      color: Theme.of(context).colorScheme.primary),
  keyBoardType: TextInputType.phone,
  hintText: 'login_phone_number_hint'.tr,
  onChanged: onChangedTextField,
),
SizedBox(
  height: GeneralHelper.scale(context: context, height: 48),
),

TextFormFieldRounded(
  radius: GeneralHelper.scale(context: context, height: 39.5),
  backgroundColor: const Color(0xFFE3F4E1),
  elevation: 0,
  hintStyle: TextStyle(
    color: const Color(0xFF81C59E),
    fontSize: GeneralHelper.scale(context: context, height: 16),
    fontWeight: FontWeight.w500,
  ),
  controller: txtPassword,
  hintText: 'login_password_hint'.tr,
  keyBoardType: TextInputType.text,
  obscureText: !isShowPassword,
  borderSize: BorderSide(
      width: 0.5,
      style: BorderStyle.solid,
      color: Theme.of(context).colorScheme.primary),
),
解决方案

问题根源是TextStyle的写法错误:你把height参数错误嵌套到了fontSize的GeneralHelper.scale调用里,导致fontSize被错误赋值为行高的缩放值,而TextStyle本身的行高属性未正确配置。

正确写法需将fontSize和height作为TextStyle的独立属性分别设置:

TextFormField(
  maxLines: 1,
  scrollPadding: EdgeInsets.zero,
  validator: validator,
  readOnly: readOnly ?? false,
  onChanged: onChanged,
  controller: controller,
  obscureText: obscureText ?? false,
  keyboardType: keyBoardType,
  style: TextStyle(
    color: Colors.black,
    // 单独设置字体大小,根据输入框类型区分
    fontSize: GeneralHelper.scale(
      context: context,
      height: obscureText == true ? 30 : 16,
    ),
    // 行高为比值(行高像素值/字号),可根据需求调整
    height: obscureText == true ? 1.0 : 17/16,
  ),
)

注意:TextStyle的height是行高与字号的比值,不是绝对像素值。比如字号30、行高30时,height设为1.0;字号16、行高17时,height设为17/16≈1.0625。调整后文本即可根据字号正确居中。

内容的提问来源于stack exchange,提问作者I am I.A.S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 03:55:25