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

Flutter中如何设置PinFieldAutoFill的选中与启用状态边框颜色

为PinFieldAutoFill设置选中与启用状态的边框颜色

要实现选中(聚焦)和启用状态的边框颜色区分,你可以利用BoxLooseDecoration的strokeColorBuilder属性,通过PinListenBuilder根据组件状态动态返回对应颜色:

PinFieldAutoFill(
  currentCode: codevalue,
  autoFocus: true,
  focusNode: myfocusnode,
  codeLength: 6,
  // 可选:添加enabled属性控制组件启用/禁用
  // enabled: _isEnabled,
  cursor: Cursor(
    width: 2,
    height: 20,
    color: Colors.red,
    radius: Radius.circular(1),
    enabled: true,
  ),
  textInputAction: TextInputAction.done,
  decoration: BoxLooseDecoration(
    radius: Radius.circular(10.0),
    bgColorBuilder: FixedColorBuilder(Color.fromARGB(255, 255, 255, 255)),
    // 动态设置边框颜色
    strokeColorBuilder: PinListenBuilder(
      onPinChange: (pin) {},
      builder: (context, pin, isFocused) {
        // 若设置了enabled属性,替换为_isEnabled判断
        bool isEnabled = true;
        
        if (isFocused && isEnabled) {
          // 选中(聚焦)状态的边框颜色
          return Colors.blue;
        } else if (isEnabled) {
          // 启用但未聚焦状态的边框颜色
          return Colors.black;
        } else {
          // 禁用状态的边框颜色
          return Colors.grey.shade400;
        }
      },
    ),
    gapSpace: 16,
  ),
)

核心说明:

  • PinListenBuilder的builder方法提供了isFocused参数,直接判断组件是否处于聚焦状态。
  • 如果需要手动控制组件启用/禁用,给PinFieldAutoFill添加enabled属性后,在builder中结合该值判断即可。

内容的提问来源于stack exchange,提问作者Hari Haran

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 14:55:20