搜尋套件、儲存庫與 APP 分類

crop_your_image 封面
套件詳情v2.0.1

crop_your_image

crop_your_image 幫助您的應用嵌入用於裁剪圖像的 Widget。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
19.6萬
喜歡數
583
Pub 分數
160

安裝套件

flutter pub add crop_your_image

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

crop_your_image 是一個 Flutter 套件,讓開發者可以使用可自訂的組件將影像裁切功能整合到他們的應用程式中。它提供了一種簡單的方式,可在應用程式介面內直接選擇和裁切影像,支援多種平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。此套件設計為易於使用,設定簡單,可輕鬆整合至現有專案。它利用 Flutter 的元件系統,提供彈性且具回應性的裁切體驗。

適用情境

  • 照片應用中的圖像編輯
  • 使用者個人檔案圖片裁剪
  • 內容創作工具
  • 社交媒體上傳預處理
  • 文件影像裁切

主要特色

  • 內建裁剪控制項
  • 支援多個平台
  • 可自定義的長寬比
  • 響應式UI設計
  • 與 Flutter 的輕鬆整合
  • 即時預覽

適合對象

  • 開發圖像密集型應用的開發者
  • 需要用戶圖像自定義的應用程式
  • 跨平台圖像處理功能
  • 專注於使用者體驗的前端團隊
  • 行動和網路應用

使用建議

  • 需要額外的權限才能存取影像
  • 可能需要自訂樣式以保持外觀一致
  • 僅限基本裁剪,不支援進階濾鏡
  • 依賴外部相依性以實現完整功能
  • 未針對非常大的圖像進行優化

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

crop_your_image

A flutter plugin that provides Crop widget for cropping images.

Image Cropping Preview

Philosophy

crop_your_image provides flexible and customizable Crop widget that can be placed at anywhere in your well designed apps.

As Crop is a simple widget displaying minimum cropping UI, Crop can be placed anywhere such as, for example, occupying entire screen, at top half of the screen, or even on dialogs or bottom sheets. It's totally up to you!

Users' cropping operation is also customizable. By default, images are fixed on the screen and users can move crop rect to decide where to crop. Once configured interactive mode, images can be zoomed/panned, and crop rect can be configured as fixed.

CropController enables you to control crop rect from outside of Crop. The controller allows you to run cropping (or related actions) from anywhere on your codebase.

Enjoy building your own cropping UI with crop_your_image!

Features

  • Minimum UI restrictions
  • Flexible Crop widget that can be placed anywhere on your widget tree
  • CropController to control Crop
  • Zooming / panning images
  • Crop with rect or circle whichever you want
  • Fix aspect ratio
  • Configure Rect of crop rect programmatically
  • Detect events of users' operation
  • Undo / Redo operation
  • (advanced) Cropping backend logics are also customizable

Note that this package DON'T

  • read / download image data from any storages, such as gallery, internet, etc.
  • resize, tilt, or other conversions which can be done with image package directly.
  • provide UI parts other than cropping editor, such as buttons to control. Building UI is fully UP TO YOU!

Usage

Basics

Place Crop Widget wherever you want to place image cropping UI.

final _controller = CropController();

@override
Widget build(BuildContext context) {
  return Crop(
    image: _imageData, 
    controller: _controller,
    onCropped: (result) {
      switch(result) {
        case CropResult.success(:final croppedImage):
          // do something with cropped image data 
        case CropResult.error(:final error):
          // do something with error
      }
    }
  );
}

Then, Crop widget will automatically display cropping editor UI on users screen with given image.

By passing CropController instance to controller argument of Crop's constructor, you can control the Crop widget from anywhere on your source code.

For example, when you want to crop the image with the current crop rect, you can just call _controller.crop() whenever you want, such like the code below.

ElevatedButton(
  child: Text('Crop it!')
  onPressed: () => _cropController.crop(),
),

Because _controller.crop() only kicks the cropping process, this method returns immediately without any cropped image data. You can obtain the result of cropping images via onCropped callback of Crop Widget.

Undo / Redo

CropController also provides undo and redo methods.

ElevatedButton(
  child: Text('Undo')
  onPressed: () => _cropController.undo(),
),

You can also detect history of crop rect changes via onHistoryChanged callback of Crop Widget. This callback exposes History object, which contains undoCount and redoCount, that indicates how many times undo / redo operation is available.

Crop(
  onHistoryChanged: (history) {
    setState(() {
      _undoEnabled = history.undoCount > 0;
      _redoEnabled = history.redoCount > 0;
    });
  },
)
List of configurations

All the arguments of Crop and usages are below.

final _controller = CropController();

Widget build(BuildContext context) {
  return Crop(
    image: _imageData,
    controller: _controller,
    onCropped: (result) {
      switch(result) {
        case CropResult.success(:final croppedImage):
          // do something with cropped image data 
        case CropResult.error(:final error):
          // do something with error
      }
    },
    aspectRatio: 4 / 3,

    initialRectBuilder: InitialRectBuilder.withBuilder((viewportRect, imageRect) {
      return Rect.fromLTRB(
        viewportRect.left + 24,
        viewportRect.top + 32,
        viewportRect.right - 24,
        viewportRect.bottom - 32,
      );
    }),
    // initialRectBuilder: InitialRectBuilder.withArea(
    //   ImageBasedRect.fromLTWH(240, 212, 800, 600),
    // ),
    // initialRectBuilder: InitialRectBuilder.withSizeAndRatio(
    //   size: 0.5,
    //   aspectRatio: 4 / 3,
    // ),

    // withCircleUi: true,
    baseColor: Colors.blue.shade900,
    maskColor: Colors.white.withAlpha(100),
    overlayBuilder: (context, rect) {
      return CustomPaint(painter: MyPainter(rect));
    },
    progressIndicator: const CircularProgressIndicator(),
    radius: 20,
    onMoved: (newRect) {
      // do something with current crop rect.
    },
    onImageMoved: (newImageRect) {
      // do something with current image rect.
    },
    onStatusChanged: (status) {
      // do something with current CropStatus
    },
    willUpdateScale: (newScale) {
      // if returning false, scaling will be canceled
      return newScale < 5;
    },
    cornerDotBuilder: (size, edgeAlignment) => const DotControl(color: Colors.blue),
    clipBehavior: Clip.none,
    interactive: true,
    // fixCropRect: true,

    // formatDetector: (image) {},
    // imageCropper: myCustomImageCropper,
    // imageParser: (image, {format}) {},
  );
}
argument type description
image Uint8List Original image data to be cropped. The result of cropping operation can be obtained via onCropped callback.
onCropped void Function(CropResult) Callback called when cropping operation is completed. The result is exposed as CropResult object. CropResult.success() contains cropped image data, and CropResult.error() contains error object.
controller CropController Controller for managing cropping operation.
aspectRatio double? Initial aspect ratio of crop rect. Set null or just omit if you want to crop images with any aspect ratio. aspectRatio can be changed dynamically via setter of CropController.aspectRatio. (see below)
initialSize double? is the initial size of crop rect. 1.0 (or null, by default) fits the size of image, which means crop rect extends as much as possible. 0.5 would be the half. This value is also referred when aspectRatio changes via CropController.aspectRatio.
initialRectBuilder InitialRectBuilder? An object preserving configuration for building the initial crop rect. InitialRectBuilder.withBuilder enables you to configure the rect with a function to decide initial Rect of crop rect based on viewport of Crop itself. InitialRectBuilder.withArea enables you to configure the rect with ImageBasedRect which is based on actual image size. InitialRectBuilder.withSizeAndRatio enables you to configure the rect with size and aspectRatio.
withCircleUi bool Flag to decide the shape of cropping UI. If true, the shape of cropping UI is circle and aspectRatio is automatically set 1.0. Note that this flag does NOT affect to the result of cropping image. If you want cropped images with circle shape, call CropController.cropCircle instead of CropController.crop.
maskColor Color? Color of the mask widget which is placed over the cropping editor.
baseColor Color? Color of the base color of the cropping editor.
overlayBuilder Widget Function(BuildContext, ViewportBasedRect)? Builder function to build Widget placed over the cropping rect. rect of argument is current ViewportBasedRect of crop rect.
radius double? Corner radius of crop rect.
onMoved void Function(ViewportBasedRect)? Callback called when crop rect is moved regardless of its reasons. newRect of argument is current ViewportBasedRect of crop rect.
onImageMoved void Function(ViewportBasedRect)? Callback called when image is moved regardless of its reasons. newImageRect of argument is current ViewportBasedRect of image.
onHistoryChanged void Function(History)? Callback called when history of crop rect is changed. This history allows you to undo / redo feature is available or not.
onStatusChanged void Function(CropStatus)? Callback called when status of Crop is changed.
willUpdateScale bool Function(double)? Callback called before scale changes on interactive mode. By returning false to this callback, updating scale will be canceled.
cornerDotBuilder Widget Function(Size, EdgeAlignment)? Builder function to build Widget placed at four corners used to move crop rect. The builder passes size which widget must follow and edgeAlignment which indicates the position.
progressIndicator Widget? Widget for showing preparing image is in progress. Nothing (SizedBox.shrink() actually) is shown by default.
interactive bool? Flag to enable interactive mode that users can move / zoom images. false by default
fixCropRect bool? Flag if crop rect should be fixed on interactive mode. false by default
clipBehavior Clip? Decide clipping strategy for Crop. Clip.hardEdge by default
filterQuality FilterQuality? Decide filter quality for Image showing target image. FilterQuality.low by default
for Web
argument type description
scrollZoomSensitivity double? Sensitivity for zoom gesture using mouse-wheel. For web applications only.
Advanced

crop_your_image also allows you to customize backend logic for

  • detecting the format of the image
  • detecting detail information (width / height) of the image
  • cropping

by passing the arguments below.

argument type description
formatDetector ImageFormat Function(Uint8List)? Function to detect the format of original image. By detecting the format before imageParser parses the original image from Uint8List to ImageDetail, imageParser will sufficiently parse the binary, which means the initializing operation speeds up. defaultFormatDetector is used by default
imageParser ImageDetail Function(Uint8List, {ImageFormat})? Function for parsing original image from Uint8List to ImageDetail, which preserve height, width and parsed image with generic type <T>. image is passed to imageCropper with Rect to be cropped. defaultImageParser is used by default
imageCropper ImageCropper? By implementing ImageCropper<T> and passing its instance to this argument, you can exchange cropping logic. defaultImageCropper is used by default

Gallery App

The repository below is for a sample app of using crop_your_image. | chooyan-eng/crop_your_image_gallery

You can find several examples with executable source codes here.

Contact

If you have anything you want to inform me (@chooyan-eng), such as suggestions to enhance this package or functionalities you want etc, feel free to make issues on GitHub or send messages on X @tsuyoshi_chujo (Japanese @chooyan_i18n).

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案