Flutter 剪裁(Clip)

🔥 ClipOval 🔥

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

裁剪纯色背景

Dart 复制代码
ClipOval(
            child: Container(
              width: 300.w,
              height: 300.w,
              decoration: const BoxDecoration(color: Colors.red),
            ),
          ),

裁剪背景图片

裁剪前

Dart 复制代码
ClipOval(
              clipBehavior: Clip.none,
              child: Image.asset(
                'assets/demo/message.png',
                width: 300.w,
              )),


裁剪后

Dart 复制代码
ClipOval(
            
              child: Image.asset(
                'assets/demo/message.png',
                width: 300.w,
              )),


自定义裁剪区域一

Dart 复制代码
ClipOval(
              clipper: ClipperOvalPath(),
              child: Image.asset(
                'assets/demo/message.png',
                width: 300.w,
              )),
Dart 复制代码
class ClipperOvalPath extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return Rect.fromLTRB(0, 0, size.width - 100.w, size.height - 100.w);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    return false;
  }
}


自定义裁剪区域二

Dart 复制代码
class ClipperOvalPath extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return Rect.fromLTRB(0, 0, size.width - 50.w, size.height - 100.w);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    return false;
  }
}


自定义裁剪区域三

Dart 复制代码
class ClipperOvalPath extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return Rect.fromLTRB(0, 0, size.width - 50.w, size.height - 200.w);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    return false;
  }
}

🔥 ClipRRect 🔥

将子组件剪裁为圆角矩形

纯色背景裁剪为圆角矩形

Dart 复制代码
ClipRRect(
          borderRadius: BorderRadius.circular(50.w),
          child: Container(
            width: 300.w,
            height: 300.w,
            decoration: const BoxDecoration(color: Colors.red),
          ),
        )

将图片裁剪为圆角矩形

Dart 复制代码
ClipRRect(
          clipper: ClipperOvalPath(),
            child: Image.asset(
              'assets/demo/message.png',
            )
        ),
Dart 复制代码
class ClipperOvalPath extends CustomClipper<RRect> {
  @override
  RRect getClip(Size size) {
    return RRect.fromLTRBR(0, 0, size.width-100.w, size.height-140.w,Radius.circular(20.w));
  }

  @override
  bool shouldReclip(covariant CustomClipper<RRect> oldClipper) {
    return true;
  }
}

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

Dart 复制代码
class ClipperOvalPath extends CustomClipper<RRect> {
  @override
  RRect getClip(Size size) {
    return RRect.fromLTRBR(0, 0, size.width-100.w, size.height-80.w,Radius.circular(20.w));
  }

  @override
  bool shouldReclip(covariant CustomClipper<RRect> oldClipper) {
    return true;
  }
}

🔥 ClipRect 🔥

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

Dart 复制代码
 Align(
            alignment: Alignment.topLeft,
            widthFactor: .5, //宽度设为原来宽度一半,另一半会溢出
            child: Image.asset("assets/demo/message.png"),
          ),
          ClipRect(//将溢出部分剪裁
            child: Align(
              alignment: Alignment.topLeft,
              widthFactor: .5,//宽度设为原来宽度一半
              child: Image.asset("assets/demo/message.png"),
            ),
          ),

🔥 ClipPath 🔥

按照自定义的路径剪裁
设置了剪切的区域

Dart 复制代码
Image.asset("assets/demo/message.png"),
          DecoratedBox(
            decoration: const BoxDecoration(color: Colors.red),
            child: ClipRect(
                clipper: MyClipper(), //使用自定义的clipper
                child: Image.asset("assets/demo/message.png")),
          )
Dart 复制代码
class MyClipper extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) => const Rect.fromLTWH(10.0, 15.0, 100.0, 200.0);

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) => false;
}
相关推荐
喵个咪6 小时前
GoWind ShortPlay 拆解:Flutter 沉浸式 Feed 的播放器工程——生命周期、手势状态机与游客降级
前端·flutter·架构
恋猫de小郭1 天前
Flutter 的文字为什么比 SwiftUI 更细?问题主要在 Skia 的 256 项查找表里
android·前端·flutter
好的佩奇2 天前
Dart 之类型系统
flutter·dart
恋猫de小郭2 天前
Dart 3.15 新特性:this 终于支持类型提升了
android·前端·flutter
FungLeo3 天前
成为全栈·Flutter App 篇·主题与可访问性:深浅色、大字号和触控体验
flutter·移动开发·深色模式·可访问性·成为全栈·大字号适配
用户95421573334854 天前
只是选个照片,怎么留下了这么多访问记录?Flutter 图片选择器排查与优化实录
flutter
FungLeo4 天前
成为全栈·Flutter App 篇·第四批复盘:复用了什么,移动端又新增了什么
flutter·移动开发·跨端开发·成为全栈·api契约·全栈复盘
jason.zeng@15022074 天前
使用vscode+flutter开发android程序(入门)
android·vscode·flutter
jason.zeng@15022074 天前
Flutter 项目标准分层目录(repository/dto/page,对齐后端思想)
flutter