使用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后,未配置错误状态下的填充色和显式指定错误边框颜色,导致包使用默认错误样式。按以下步骤修改:
- 配置错误状态样式:取消
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, ),
- 修正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
相关产品推荐
相关产品推荐

