• 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. }

  • 相关阅读:
    NoSQL之readis配置与优化(启示录)
    昆仑万维旗下StarMaker VR成功加入Oculus开发者计划 获Oculus资金支持
    项目实训-vue(十二)
    【描述一下 V8 执行一段JS代码的过程?】
    MyBatis的缓存
    罗克韦尔AB PLC Logix5000中如何创建标签并使用标签进行编程?
    【软件测试】接手一个测试任务,怎么快速开始测试快速找出测试点?(总结)
    C++——关键字|命名空间|输入&输出|缺省参数|函数重载|引用
    提升Mac运行速度的十大小技巧,你用过几个?
    接口自动化测试实战:JMeter+Ant+Jenkins+钉钉机器人群通知完美结合
  • 原文地址:https://blog.csdn.net/u013491829/article/details/133822993