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

使用pin_code_fields包时验证环节出现异常错误边框的问题咨询

问题描述

我正在使用pin_code_fields包实现验证码输入UI,但验证生效时出现不符合预期的异常错误边框。已尝试修改enableActiveFill、PinTheme等参数,问题仍未解决。

以下是我的PinCodeTextField代码:

SizedBox(
        width: 222.w,
        child: PinCodeTextField(
          length: 4,
          controller: notifier.codeController,
          appContext: context,
          autovalidateMode: AutovalidateMode.onUserInteraction,
          onChanged: (value) {},
          enableActiveFill: true,
          useExternalAutoFillGroup: true,
          animationDuration: const Duration(milliseconds: 300),
          animationType: AnimationType.fade,
          autoFocus: true,
          cursorColor:
              Theme.of(context).extension<MyThemeExtension>()!.black,
          keyboardType: TextInputType.number,
          validator: (value) {
            if (value!.length < 4) {
              return localizations.thisFieldIsRequired;
            }
            if (int.tryParse(value!) == null) {
              return localizations.unavaiableCharacters;
            }else {
              return null;
            }
          },
          pinTheme: PinTheme(
            borderRadius: BorderRadius.circular(8.r),
            selectedColor:
                Theme.of(context).extension<MyThemeExtension>()!.greenInfo,
            activeFillColor:
                Theme.of(context).extension<MyThemeExtension>()!.white,
            activeColor:
                Theme.of(context).extension<MyThemeExtension>()!.greenInfo,
            selectedFillColor:
                Theme.of(context).extension<MyThemeExtension>()!.white,
            inactiveFillColor:
                Theme.of(context).extension<MyThemeExtension>()!.white,
            inactiveColor:
                Theme.of(context).extension<MyThemeExtension>()!.grey2,
            // errorBorderColor:
            //     Theme.of(context).extension<MyThemeExtension>()!.redDiscount,
            shape: PinCodeFieldShape.box,
            borderWidth: 1.r,
            fieldHeight: 48.r,
            fieldWidth: 48.r,
          ),
        ),
      ),

错误效果:验证码输入框异常错误边框


解决方案

问题出在开启enableActiveFill后,未配置错误状态下的填充色和显式指定错误边框颜色,导致包使用默认错误样式。按以下步骤修改:

  1. 配置错误状态样式:取消errorBorderColor的注释,并添加errorFillColor参数,保持和正常状态填充色一致:
pinTheme: PinTheme(
  borderRadius: BorderRadius.circular(8.r),
  selectedColor:
      Theme.of(context).extension<MyThemeExtension>()!.greenInfo,
  activeFillColor:
      Theme.of(context).extension<MyThemeExtension>()!.white,
  activeColor:
      Theme.of(context).extension<MyThemeExtension>()!.greenInfo,
  selectedFillColor:
      Theme.of(context).extension<MyThemeExtension>()!.white,
  inactiveFillColor:
      Theme.of(context).extension<MyThemeExtension>()!.white,
  inactiveColor:
      Theme.of(context).extension<MyThemeExtension>()!.grey2,
  errorBorderColor:
      Theme.of(context).extension<MyThemeExtension>()!.redDiscount,
  errorFillColor:
      Theme.of(context).extension<MyThemeExtension>()!.white, // 添加错误填充色
  shape: PinCodeFieldShape.box,
  borderWidth: 1.r,
  fieldHeight: 48.r,
  fieldWidth: 48.r,
),
  1. 修正validator冗余空安全判断:去掉int.tryParse(value!)中的多余!,因为前面已经通过value!.length确保value不为空:
validator: (value) {
  if (value!.length < 4) {
    return localizations.thisFieldIsRequired;
  }
  if (int.tryParse(value) == null) {
    return localizations.unavaiableCharacters;
  } else {
    return null;
  }
},

内容的提问来源于stack exchange,提问作者Morgan Windy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 12:22:56