Editor.crop constructor

Editor.crop({
  1. Image? image,
  2. CropImageLayer? layer,
  3. DrawImageLayer? draw,
  4. EntityLayer? entities,
  5. required ValueListenable<Matrix4> camera,
  6. required AsyncCallback onSubmit,
  7. required AsyncCallback onCancel,
})

Wraps a CropImageLayer with the crop / rotate / flip / aspect-ratio controls. The returned editor's exporter rasterises the cropped + transformed image at CropImageLayer.exportSize.

Aspect-ratio cycling is internal: each tap on the aspect-ratio button advances through [free, 1:1, 4:5, 16:9] and snaps the crop rectangle.

Pass layer to reuse a pre-existing instance (the "borrowed" mode used by CropScreen.borrowed): the editor will NOT dispose the layer on dispose, so its state survives across tool re-entries. Pass image instead to construct + own a fresh layer.

Implementation

factory Editor.crop({
  ui.Image? image,
  CropImageLayer? layer,
  DrawImageLayer? draw,
  EntityLayer? entities,
  required ValueListenable<vmath.Matrix4> camera,
  required AsyncCallback onSubmit,
  required AsyncCallback onCancel,
}) {
  assert(
    (image == null) != (layer == null),
    'Editor.crop: pass exactly one of image: or layer:',
  );
  final cropLayer = layer ?? CropImageLayer(image: image!);
  final ownsLayer = layer == null;

  final aspectRatios = <double?>[null, 1.0, 4 / 5, 16 / 9];
  final aspectIdx = ValueNotifier<int>(0);

  Future<void> handleCancel() async {
    await onCancel();
  }

  Future<void> handleSubmit() async {
    await onSubmit();
  }

  final controls = ListenableBuilder(
    listenable: cropLayer,
    builder: (context, _) => ValueListenableBuilder<int>(
      valueListenable: aspectIdx,
      builder: (context, _, _) => CropControls(
        onCancel: handleCancel,
        onRotate: cropLayer.rotate90,
        onFlip: cropLayer.flipHorizontal,
        isFlipped: cropLayer.imageTransform.flip,
        onAspectRatio: () {
          aspectIdx.value = (aspectIdx.value + 1) % aspectRatios.length;
          cropLayer.setAspectRatio(aspectRatios[aspectIdx.value]);
        },
        onSubmit: handleSubmit,
      ),
    ),
  );

  final editor = Editor(
    gestures: CropGestures(layer: cropLayer, hostCamera: camera),
    controls: controls,
    camera: camera,
    overlays: [
      CropRotationDial(layer: cropLayer, hostCamera: camera),
      CropAreaOverlay(layer: cropLayer, hostCamera: camera),
    ],
    painter: CropScenePainter(
      crop: cropLayer,
      camera: camera,
      draw: draw,
      entities: entities,
    ),
  )..add(cropLayer, owned: ownsLayer);
  if (draw != null) editor.add(draw, owned: false);
  if (entities != null) editor.add(entities, owned: false);
  return editor;
}