A pure Dart/Flutter image cropper library with no native dependencies. This package provides an interactive image cropping widget that works entirely in Dart and Flutter, making it platform-independent and easy to use.
Experience all features of the library in action with our live web demo!
β¨ Pure Dart/Flutter - No platform-specific code or native dependencies
π― Interactive Cropping - Drag corners to resize the crop area
π Aspect Ratio Support - Constrain crops to specific aspect ratios (1:1, 4:3, 16:9, etc.)
π Rotation - Rotate images by 90-degree increments
π Gesture Support - Pinch to zoom and pan the image
π¨ Customizable UI - Configure colors, grid visibility, and boundary styles
β‘ Efficient - Uses the image package for fast image processing in Dart
Add this to your package's pubspec.yaml file:
dependencies:
flutterish_image_cropper: ^0.1.0Then run:
flutter pub getimport 'package:flutter/material.dart';
import 'package:flutterish_image_cropper/flutterish_image_cropper.dart';
import 'dart:ui' as ui;
class MyCropperPage extends StatefulWidget {
@override
_MyCropperPageState createState() => _MyCropperPageState();
}
class _MyCropperPageState extends State<MyCropperPage> {
final CropController _controller = CropController();
@override
void initState() {
super.initState();
_loadImage();
}
Future<void> _loadImage() async {
// Load your image as ui.Image
// You can use image_picker, assets, or network images
ui.Image image = ...; // Load your image here
_controller.setImage(image);
}
Future<void> _cropImage() async {
final result = await _controller.crop();
if (result != null) {
// Use the cropped image
print('Cropped image size: ${result.width}x${result.height}');
// result.imageBytes contains the PNG bytes
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Cropper'),
actions: [
IconButton(
icon: Icon(Icons.crop),
onPressed: _cropImage,
),
],
),
body: ImageCropper(
controller: _controller,
aspectRatio: 1.0, // Square crop
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}// Free crop (no constraint)
ImageCropper(controller: controller, aspectRatio: null)
// Square crop
ImageCropper(controller: controller, aspectRatio: 1.0)
// 16:9 crop
ImageCropper(controller: controller, aspectRatio: 16/9)
// 4:3 crop
ImageCropper(controller: controller, aspectRatio: 4/3)// Rotate 90 degrees clockwise
_controller.rotateRight();
// Rotate 90 degrees counter-clockwise
_controller.rotateLeft();ImageCropper(
controller: _controller,
backgroundColor: Colors.black,
overlayColor: Color(0x88000000),
boundaryColor: Colors.white,
boundaryWidth: 2.0,
showGrid: true,
aspectRatio: 16/9,
)The main controller for managing crop state and operations.
Methods:
setImage(ui.Image image)- Set the image to cropsetCropRect(Rect rect)- Set the crop rectangle (normalized coordinates 0-1)setRotation(double rotation)- Set rotation in radianssetScale(double scale)- Set zoom scale (0.5 - 5.0)setOffset(Offset offset)- Set pan offsetsetAspectRatio(double? ratio)- Set aspect ratio constraintrotateRight()- Rotate 90Β° clockwiserotateLeft()- Rotate 90Β° counter-clockwiseFuture<CropResult?> crop()- Perform the crop and return result
The interactive cropping widget.
Parameters:
controller(required) - The CropControllerbackgroundColor- Background color (default: Colors.black)overlayColor- Overlay color for non-crop areas (default: Color(0x88000000))boundaryColor- Color of crop boundaries (default: Colors.white)boundaryWidth- Width of boundary lines (default: 2.0)showGrid- Show grid lines (default: true)aspectRatio- Aspect ratio constraint (default: null)
The result of a crop operation.
Properties:
imageBytes- Uint8List containing PNG image datawidth- Width of cropped image in pixelsheight- Height of cropped image in pixels
This library uses pure Dart/Flutter technologies:
- UI Layer: Flutter's CustomPaint and GestureDetector for the interactive cropping interface
- Image Processing: The
imagepackage for decoding, transforming, and encoding images - No Platform Channels: Everything runs in Dart, making it truly cross-platform
The cropping process:
- User interacts with the crop area using gestures
- Crop rectangle coordinates are maintained in normalized space (0-1)
- When crop() is called, the image is processed using the
imagepackage - Rotation and cropping are applied in Dart
- Result is encoded as PNG bytes
Check out the example directory for a complete demonstration app showing all features including:
- Loading images
- Interactive cropping with gestures
- Aspect ratio switching
- Rotation controls
- Displaying cropped results
To run the example:
cd example
flutter runβ
Android
β
iOS
β
Web
β
macOS
β
Linux
β
Windows
Works on all Flutter platforms since it uses no native code!
# Run all tests
flutter test
# Run tests with coverage
flutter test --coverage
# Run only golden tests
flutter test test/image_cropper_golden_test.dartThis project includes golden (snapshot) tests to ensure UI consistency. To update golden files after intentional UI changes:
flutter test --update-goldensSee test/goldens/README.md for more information about golden tests.
Before submitting a PR, ensure your code passes all checks:
# Format code
dart format .
# Analyze code
flutter analyze
# Run tests
flutter testThe CI workflow will automatically run these checks on all pull requests.
MIT License - see LICENSE file for details
Contributions are welcome! Please feel free to submit a Pull Request.