You need to enable JavaScript to run this app. Flutter 自定义TextFormField侧边差异化宽度边框(不使用Container实现)
优惠活动
大模型
产品
解决方案
定价
更多

Flutter 自定义TextFormField侧边差异化宽度边框(不使用Container实现)

不使用 Container 实现 TextFormField 分侧边自定义边框宽度

核心实现逻辑是复用 TextFormField 自带的边框绘制通道,通过自定义继承 InputBorder 的类实现逐边控制宽度,完全不需要额外用组件包裹输入框。

实现步骤

  • 自定义支持分侧边宽度的边框类
    继承 InputBorder 基类,内置四个边的宽度参数,重写绘制方法逐边渲染对应宽度的色块,适配输入框的不同交互状态(聚焦、禁用、报错)。
    完整代码:
    class SideWidthInputBorder extends InputBorder {
      const SideWidthInputBorder({
        required this.sideWidths,
        this.borderColor = const Color(0xFF9E9E9E),
      });
    
      // 用EdgeInsets存储四个边的宽度,零值对应不绘制该边
      final EdgeInsets sideWidths;
      final Color borderColor;
    
      @override
      EdgeInsetsGeometry get dimensions => sideWidths;
    
      @override
      void paint(
        Canvas canvas,
        Rect rect, {
        double? gapStart,
        double gapExtent = 0.0,
        double gapPercentage = 0.0,
        TextDirection? textDirection,
      }) {
        // 绘制左边框
        if (sideWidths.left > 0) {
          canvas.drawRect(
            Rect.fromLTWH(
              rect.left,
              rect.top,
              sideWidths.left,
              rect.height,
            ),
            Paint()..color = borderColor,
          );
        }
        // 绘制右边框
        if (sideWidths.right > 0) {
          canvas.drawRect(
            Rect.fromLTWH(
              rect.right - sideWidths.right,
              rect.top,
              sideWidths.right,
              rect.height,
            ),
            Paint()..color = borderColor,
          );
        }
        // 绘制上边框
        if (sideWidths.top > 0) {
          canvas.drawRect(
            Rect.fromLTWH(
              rect.left,
              rect.top,
              rect.width,
              sideWidths.top,
            ),
            Paint()..color = borderColor,
          );
        }
        // 绘制下边框
        if (sideWidths.bottom > 0) {
          canvas.drawRect(
            Rect.fromLTWH(
              rect.left,
              rect.bottom - sideWidths.bottom,
              rect.width,
              sideWidths.bottom,
            ),
            Paint()..color = borderColor,
          );
        }
      }
    
      @override
      InputBorder scale(double t) {
        return SideWidthInputBorder(
          sideWidths: sideWidths * t,
          borderColor: borderColor,
        );
      }
    
      @override
      InputBorder copyWith() {
        return SideWidthInputBorder(
          sideWidths: sideWidths,
          borderColor: borderColor,
        );
      }
    
      @override
      bool operator ==(Object other) {
        if (identical(this, other)) return true;
        return other is SideWidthInputBorder &&
            other.sideWidths == sideWidths &&
            other.borderColor == borderColor;
      }
    
      @override
      int get hashCode => Object.hash(sideWidths, borderColor);
    }
    
  • 给 TextFormField 传入自定义边框
    直接将自定义边框赋值给 InputDecoration 的对应状态属性,不需要额外包裹 Container 或其他布局组件,示例实现左边框宽3、其余边宽1的效果:
    TextFormField(
      decoration: InputDecoration(
        // 对应不同交互状态分别传入自定义边框
        border: SideWidthInputBorder(
          sideWidths: const EdgeInsets.fromLTRB(3, 1, 1, 1),
        ),
        enabledBorder: SideWidthInputBorder(
          sideWidths: const EdgeInsets.fromLTRB(3, 1, 1, 1),
        ),
        focusedBorder: SideWidthInputBorder(
          sideWidths: const EdgeInsets.fromLTRB(3, 1, 1, 1),
          borderColor: Colors.blue, // 聚焦态切换边框颜色
        ),
        contentPadding: const EdgeInsets.symmetric(
          horizontal: 16,
          vertical: 12,
        ),
        hintText: '请输入内容',
      ),
    )
    

实现说明

  • 该方案完全走 Flutter 输入组件原生的边框渲染逻辑,不会打断输入框的事件传递、焦点交互,也不会产生额外的布局层级
  • 如果需要加圆角效果,只需要在 paint 方法中将 drawRect 替换为 drawRRect,传入自定义的圆角半径即可
  • 扩展时可以新增参数为四个边分别设置不同的颜色,不需要改动核心绘制逻辑

内容的提问来源于stack exchange,提问作者The boring way

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.10 16:24:06