• Flutter 剪裁(Clip)


    🔥 ClipOval 🔥

    子组件为正方形时剪裁成内贴圆形;为矩形时,剪裁成内贴椭圆

    裁剪纯色背景 

    1. ClipOval(
    2. child: Container(
    3. width: 300.w,
    4. height: 300.w,
    5. decoration: const BoxDecoration(color: Colors.red),
    6. ),
    7. ),

    裁剪背景图片

    裁剪前 

    1. ClipOval(
    2. clipBehavior: Clip.none,
    3. child: Image.asset(
    4. 'assets/demo/message.png',
    5. width: 300.w,
    6. )),

    裁剪后

    1. ClipOval(
    2. child: Image.asset(
    3. 'assets/demo/message.png',
    4. width: 300.w,
    5. )),

    自定义裁剪区域一

    1. ClipOval(
    2. clipper: ClipperOvalPath(),
    3. child: Image.asset(
    4. 'assets/demo/message.png',
    5. width: 300.w,
    6. )),
    1. class ClipperOvalPath extends CustomClipper<Rect> {
    2. @override
    3. Rect getClip(Size size) {
    4. return Rect.fromLTRB(0, 0, size.width - 100.w, size.height - 100.w);
    5. }
    6. @override
    7. bool shouldReclip(CustomClipper oldClipper) {
    8. return false;
    9. }
    10. }

    自定义裁剪区域二

    1. class ClipperOvalPath extends CustomClipper<Rect> {
    2. @override
    3. Rect getClip(Size size) {
    4. return Rect.fromLTRB(0, 0, size.width - 50.w, size.height - 100.w);
    5. }
    6. @override
    7. bool shouldReclip(CustomClipper oldClipper) {
    8. return false;
    9. }
    10. }

    自定义裁剪区域三 

    1. class ClipperOvalPath extends CustomClipper<Rect> {
    2. @override
    3. Rect getClip(Size size) {
    4. return Rect.fromLTRB(0, 0, size.width - 50.w, size.height - 200.w);
    5. }
    6. @override
    7. bool shouldReclip(CustomClipper oldClipper) {
    8. return false;
    9. }
    10. }

     

    🔥 ClipRRect 🔥

    将子组件剪裁为圆角矩形

    纯色背景裁剪为圆角矩形

    1. ClipRRect(
    2. borderRadius: BorderRadius.circular(50.w),
    3. child: Container(
    4. width: 300.w,
    5. height: 300.w,
    6. decoration: const BoxDecoration(color: Colors.red),
    7. ),
    8. )

     将图片裁剪为圆角矩形

    1. ClipRRect(
    2. clipper: ClipperOvalPath(),
    3. child: Image.asset(
    4. 'assets/demo/message.png',
    5. )
    6. ),
    1. class ClipperOvalPath extends CustomClipper<RRect> {
    2. @override
    3. RRect getClip(Size size) {
    4. return RRect.fromLTRBR(0, 0, size.width-100.w, size.height-140.w,Radius.circular(20.w));
    5. }
    6. @override
    7. bool shouldReclip(covariant CustomClipper oldClipper) {
    8. return true;
    9. }
    10. }

    自定义裁剪区域导致裁剪图片为圆角矩形失败 

    1. class ClipperOvalPath extends CustomClipper<RRect> {
    2. @override
    3. RRect getClip(Size size) {
    4. return RRect.fromLTRBR(0, 0, size.width-100.w, size.height-80.w,Radius.circular(20.w));
    5. }
    6. @override
    7. bool shouldReclip(covariant CustomClipper oldClipper) {
    8. return true;
    9. }
    10. }

    🔥 ClipRect 🔥

    默认剪裁掉子组件布局空间之外的绘制内容(溢出部分剪裁)

    1. Align(
    2. alignment: Alignment.topLeft,
    3. widthFactor: .5, //宽度设为原来宽度一半,另一半会溢出
    4. child: Image.asset("assets/demo/message.png"),
    5. ),
    6. ClipRect(//将溢出部分剪裁
    7. child: Align(
    8. alignment: Alignment.topLeft,
    9. widthFactor: .5,//宽度设为原来宽度一半
    10. child: Image.asset("assets/demo/message.png"),
    11. ),
    12. ),

     🔥 ClipPath 🔥

    按照自定义的路径剪裁

    设置了剪切的区域

    1. Image.asset("assets/demo/message.png"),
    2. DecoratedBox(
    3. decoration: const BoxDecoration(color: Colors.red),
    4. child: ClipRect(
    5. clipper: MyClipper(), //使用自定义的clipper
    6. child: Image.asset("assets/demo/message.png")),
    7. )
    1. class MyClipper extends CustomClipper<Rect> {
    2. @override
    3. Rect getClip(Size size) => const Rect.fromLTWH(10.0, 15.0, 100.0, 200.0);
    4. @override
    5. bool shouldReclip(CustomClipper oldClipper) => false;
    6. }

  • 相关阅读:
    Java —— 抽象类和接口
    vue开启splitChunks分包处理
    leetcode第362场周赛
    一个Behance被封,还有无数国内网站给我灵感
    [附源码]计算机毕业设计JAVA音乐网站
    微服务架构的可观察性设计模式
    FastReID 从pytorch到caffe (一)制作自己的数据集
    Java检测是否包含首字符串startsWith() 方法
    手机如何开启开发者选项? (小米为例)
    Java接口和抽象类的区别
  • 原文地址:https://blog.csdn.net/u013491829/article/details/133822993