CropBox constructor

CropBox({
  1. Rect? cropRect,
  2. required Size clipSize,
  3. required Widget child,
  4. required _CropRectUpdate cropRectUpdateEnd,
  5. Function? cropRectUpdateStart,
  6. _CropRectUpdate? cropRectUpdate,
  7. Size? cropRatio,
  8. Size? maxCropSize,
  9. double maxScale = 10.0,
  10. CropBoxType cropBoxType = CropBoxType.Square,
  11. bool needInnerBorder = false,
  12. GridLine? gridLine,
  13. CropBoxBorder? cropBoxBorder,
  14. Color? backgroundColor,
  15. Color? maskColor,
})

裁剪素材组件

通过传入裁剪素材宽高clipSize,裁剪区域比例cropRatio以及待裁剪的内容child,就可以生成裁剪器,支持手势移动、放大缩小操作

再通过cropRectUpdateEnd回调拿到裁剪区域的值,对应到素材进行裁剪操作

{@tool dartpad --template=stateless_widget_material}

代码示例

CropBox(
  // cropRect: Rect.fromLTRB(1 - 0.4083, 0.162, 1, 0.3078), // 2.4倍 随机位置
  // cropRect: Rect.fromLTRB(0, 0, 0.4083, 0.1457), //2.4倍,都是0,0
  cropRect: Rect.fromLTRB(0, 0, 1, 0.3572), // 1倍
  clipSize: Size(200, 315),
  cropRatio: Size(16, 9),
  cropRectUpdateEnd: (rect) {
    print("裁剪区域移动 $rect");
  },
  child: Image.network(
    "https://img1.maka.im/materialStore/beijingshejia/tupianbeijinga/9/M_7TNT6NIM/M_7TNT6NIM_v1.jpg",
    width: double.infinity,
    height: double.infinity,
    fit: BoxFit.cover,
    loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent loadingProgress) {
      if (loadingProgress == null)
        return child;
      return Center(
        child: CircularProgressIndicator(
          value: loadingProgress.expectedTotalBytes != null
              ? loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes
              : null,
        ),
      );
    },
  ),
)

{@end-tool}

Implementation

CropBox({this.cropRect, required this.clipSize, required this.child, required this.cropRectUpdateEnd, this.cropRectUpdateStart, this.cropRectUpdate, this.cropRatio, this.maxCropSize, this.maxScale = 10.0, this.cropBoxType = CropBoxType.Square, this.needInnerBorder = false, this.gridLine, this.cropBoxBorder, this.backgroundColor, this.maskColor});