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

Flutter自定义ShapeBorder显示异常:形状固定在屏幕顶部(已解决)

自定义ShapeBorder位置错误问题解决

我为Container创建了一个自定义ShapeBorder,但使用时形状绘制在屏幕顶部。实际红色形状应该处于绿色与蓝色容器之间的空白区域。

原Shape代码

class BottomButtonShape extends ShapeBorder {
 @override
 EdgeInsetsGeometry get dimensions => EdgeInsets.only();

 @override
 ShapeBorder scale(double t) => this;

 @override
 Path getInnerPath(Rect rect, {TextDirection? textDirection}){
  Path path = Path();
 return Path();
 }

@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) {

Path path = Path();
path.lineTo(rect.width, rect.height);
path.lineTo(20, rect.height);
path.lineTo(20, 0);
path.lineTo(0,0 );

return path;
}

@override
void paint(Canvas canvas, Rect rect, {TextDirection? 
textDirection}) {
 }
 }

原Container代码

Container(
          height: 100,
          width: MediaQuery.of(context).size.width,
          decoration: ShapeDecoration(
              shape: BottomButtonShape(),
              shadows: [BoxShadow(color: AppColors.black, blurRadius: 20)],
              color: Colors.red
          ),
        ),

解决方案

问题根源在于Shape内的rect定位逻辑错误,所有坐标都应该基于rect的top、bottom等属性来计算:

  • 左上角坐标应设为rect.left、rect.top
  • 后续形状的位置需要用rect.top + 数值这类相对rect的方式来设置,而非直接使用固定的0或数值。

内容的提问来源于stack exchange,提问作者Cristian Baron

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 19:45:57