Flutter开发之——ClipPath

clipBehavior①

| 属性 | 说明 |

| :-: | :-: |

| none | 不裁剪,系统默认值 |

| hardEdge | 裁剪但不应用抗锯齿 |

| antiAlias | 裁剪而且抗锯齿 |

| antiAliasWithSaveLayer | 裁剪、抗锯齿而且有一个缓冲区 |

示例(heightFactor裁剪高度百分比)

代码

ClipRect(child: Center(

heightFactor: 0.6,

child: Container(color: Colors.grey,child: Image.asset(“images/flutter.png”),width: 150,height: 150,),

),),

裁剪前效果图

裁剪后效果图

2.2 ClipRRect-圆角裁剪

构造函数

const ClipRRect({

Key? key,

this.borderRadius = BorderRadius.zero,

this.clipper,

this.clipBehavior = Clip.antiAlias,

Widget? child,

})

属性说明
  • borderRadius:默认圆角半径是0,没有裁剪
示例

代码

ClipRRect(borderRadius: BorderRadius.circular(20), child: Image.asset(“images/rabbit.jpg”,width: 150,height: 150,)),

效果图

2.3 ClipOval—椭圆裁剪

构造函数

const ClipOval({Key? key, this.clipper, this.clipBehavior = Clip.antiAlias, Widget? child})

说明
  • 如果父组件为正方形,切出来是圆形

  • 如果父组件为长方形,切出来是椭圆形

示例

代码

ClipOval( child: Image.asset(“images/rabbit.jpg”,width: 200,height: 150,fit: BoxFit.cover)),

效果图

2.4 ClipPath.shape—路径裁剪

说明
  • 不直接使用ClipPath构造函数,而调用ClipPath.shape进行路径裁剪

  • shape参数是ShapeBorder类型,系统已经定义了很多形状

| 属性 | 说明 |

| :-: | :-: |

| RoundedRectangleBorder | 圆角矩形 |

| StadiumBorder | 足球形 |

| BeveledRectangleBorder | 斜角矩形 |

示例

代码

ClipPath.shape(

shape: StadiumBorder(),

child: Container(height: 150, width: 250, child: Image.asset(‘images/rabbit.jpg’, fit: BoxFit.cover,),

),

)

效果图

三 自定义裁剪—CustomClipper


3.1 说明

  • CustomClipper不是组件,是一个抽象类

  • clipper属性对应的就是CustomClipper,需要用户自己定义

3.2 自定义CustomClipper(三角形)

class TrianglePath extends CustomClipper {

@override

Path getClip(Size size) {

var path = Path();

path.moveTo(size.width/2, 0);

path.lineTo(0, size.height);

path.lineTo(size.width, size.height);

return path;

}

@override

bool shouldReclip(CustomClipper oldClipper) {

return true;

}

}

3.3 示例(使用TrianglePath)

代码

ClipPath(

clipper: TrianglePath(),

child: Container(height: 150, width: 250, child: Image.asset(‘images/rabbit.jpg’,fit: BoxFit.cover,),),

),

效果图

Clipper) {

return true;

}

}

3.3 示例(使用TrianglePath)

代码

ClipPath(

clipper: TrianglePath(),

child: Container(height: 150, width: 250, child: Image.asset(‘images/rabbit.jpg’,fit: BoxFit.cover,),),

),

效果图
[外链图片转存中…(img-fpUrv7Az-1738306161006)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值