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

crop_image 封面
套件詳情v1.0.17

crop_image

一個帶有90度旋轉功能的影像裁剪元件。支援行動裝置、網頁與桌面端。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
5萬
喜歡數
162
Pub 分數
160

安裝套件

flutter pub add crop_image

套件介紹

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

一個用於裁剪影像的 Flutter 小部件,支援 90 度旋轉,專為行動、網頁與桌面平台設計。它提供直覺的介面,讓使用者能選擇並調整影像裁剪區域,非常適合用於頭像編輯、照片處理與內容創作工作流程。此套件輕巧,可順暢整合至現有的 Flutter 應用程式中。

適用情境

  • 個人檔案圖片裁剪
  • 照片編輯工具
  • 內容創作應用
  • 圖像預處理
  • 使用者頭像管理

主要特色

  • 90度圖像旋轉
  • 跨平台相容性
  • 直覺式的拖曳與放置裁切
  • 可自訂的裁剪比例
  • 內建的儲存/匯出功能

適合對象

  • 開發影像編輯功能的開發者
  • 需要用戶上傳圖像處理的應用程式
  • 跨平台 Flutter 項目
  • 簡單而有效的裁剪解決方案

使用建議

  • 需要在裁剪前載入影像
  • 限制為矩形裁剪形狀
  • 沒有像濾鏡之類的進階編輯工具
  • 依賴外部圖像加載庫
  • 在某些平台可能需要額外的權限才能存取檔案

原始 README

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

Crop Image

pub package

An image cropper widget. It provides the usual user experience of classic mobile and desktop image croppers but, as it's written completely in Dart, it doesn't depend on any mobile package. Consequently, it runs on all platforms supported by Flutter: mobile, web and desktop alike.

The appearance of the crop rectangle can be customized.

final controller = CropController(
  /// If not specified, [aspectRatio] will not be enforced.
  aspectRatio: 1,
  /// Specify in percentages (1 means full width and height). Defaults to the full image.
  defaultCrop: Rect.fromLTRB(0.1, 0.1, 0.9, 0.9),
);

CropImage(
  /// Only needed if you expect to make use of its functionality like setting initial values of
  /// [aspectRatio] and [defaultCrop].
  controller: controller,
  /// The image to be cropped. Use [Image.file] or [Image.network] or any other [Image].
  image: Image.asset('...'),
  /// The crop grid color of the outer lines. Defaults to 70% white.
  gridColor: Colors.white,
  /// The crop grid color of the inner lines. Defaults to [gridColor].
  gridInnerColor: Colors.white,
  /// The crop grid color of the corner lines. Defaults to [gridColor].
  gridCornerColor: Colors.white,
  /// The size of the corner of the crop grid. Defaults to 25.
  gridCornerSize: 50,
  /// The offset of the corner handles from the crop grid edges. 
  /// Positive values move corners outside. Defaults to 0.
  cornerOffset: 10,
  /// Whether to display the corners. Defaults to true.
  showCorners: true,
  /// The width of the crop grid thin lines. Defaults to 2.
  gridThinWidth: 3,
  /// The width of the crop grid thick lines. Defaults to 5.
  gridThickWidth: 6,
  /// The crop grid scrim (outside area overlay) color. Defaults to 54% black.
  scrimColor: Colors.grey.withOpacity(0.5),
  /// True: Always show third lines of the crop grid.
  /// False: third lines are only displayed while the user manipulates the grid (default).
  alwaysShowThirdLines: true,
  /// Event called when the user changes the crop rectangle.
  /// The passed [Rect] is normalized between 0 and 1.
  onCrop: (rect) => print(rect),
  /// The minimum pixel size the crop rectangle can be shrunk to. Defaults to 100.
  minimumImageSize: 50,
  /// The maximum pixel size the crop rectangle can be grown to. Defaults to infinity.
  /// You can constrain the crop rectangle to a fixed size by setting
  /// both [minimumImageSize] and [maximumImageSize] to the same value (the width) and using
  /// the [aspectRatio] of the controller to force the other dimension (width / height).
  /// Doing so disables the display of the corners.
  maximumImageSize: 2000;
);

Setting up

Using a controller is optional, if you don't provide your own, the widget will create one and use it internally. Without a controller, you can handle the onCrop event and keep track of the changing crop selection of the user. When using a controller, you don't need to use onCrop, you can simply read out the crop values any time, also converted to pixels rather than percentage.

Initial values for aspectRatio and defaultCrop can be provided on the controller.

final controller = CropController(
  aspectRatio: 16.0 / 9.0,
  defaultCrop: Rect.fromLTRB(0.05, 0.05, 0.95, 0.95),
);

The controller also allows you to change both the aspectRatio and the crop rectangle programmatically:

controller.aspectRatio = 16.0 / 9.0;
controller.crop = Rect.fromLTRB(0.05, 0.05, 0.95, 0.95);

Note that if an aspectRatio was specified, all crop rectangles will be adjusted automatically to fit the required aspect ratio.

Fixed crop size

You can constrain the crop rectangle to a fixed size by setting both minimumImageSize and maximumImageSize to the same value (the width) and using the aspectRatio of the controller to force the other dimension (width / height). Doing so disables the display of the corners.

Rotation

You can set the rotation of the crop rectangle to a specific orientation using the provided CropRotation values:

controller.rotation = CropRotation.right;

or rotating it (repeatedly) with:

controller.rotateLeft();
controller.rotateRight();

Using the result

The final crop rectangle decided by the user can be used in various ways. The base output of the widget is the crop rectangle in relative terms, all four values of the Rect normalized to between 0 and 1 (1 meaning full width and height), so basically in percentage. The controller also has a cropSize property that maps the crop rectangle to the actual pixels of the bitmap.

Rect finalCropRelative = controller.crop;
Rect finalCropPixels = controller.cropSize;

The widget does not crop the original bitmap directly but its CropController provides two convenience functions to do so if required:

ui.Image bitmap = await controller.croppedBitmap();
Image image = await controller.croppedImage();

If you want to create an Image with additional settings (eg. different Image.fit), replicate the functionality of croppedImage() for yourself. You can use the UiImageProvider image provider exposed from the package.

Creating a file

If you want to save the cropped ui.Image to a file, this operation is not specific to this plugin. Use the existing functions of ui.Image and File:

data = await bitmap.toByteData(format: ImageByteFormat.png);
bytes = data!.buffer.asUint8List();
file.writeAsBytes(bytes, flush);

Known problems

croppedBitmap() – and consequently, croppedImage() – result in an exception on Flutter Web with the HTML web renderer. The culprit is Picture.toImage() that doesn't work with it (see https://github.com/flutter/engine/pull/20750). Consider using CanvasKit for the web renderer (which is much better than HTML, anyway).

Support

If you like this package, please consider supporting it.

Buy Me A Book

所屬分類

原始碼儲存庫

使用此套件的專案