Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add image rotation, resolve #73 #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions lib/src/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Crop extends StatefulWidget {
final double maximumScale;
final bool alwaysShowGrid;
final ImageErrorListener? onImageError;
final double rotation;

const Crop({
Key? key,
Expand All @@ -27,6 +28,7 @@ class Crop extends StatefulWidget {
this.maximumScale = 2.0,
this.alwaysShowGrid = false,
this.onImageError,
this.rotation = 0,
}) : super(key: key);

Crop.file(
Expand All @@ -37,6 +39,7 @@ class Crop extends StatefulWidget {
this.maximumScale = 2.0,
this.alwaysShowGrid = false,
this.onImageError,
this.rotation = 0,
}) : image = FileImage(file, scale: scale),
super(key: key);

Expand All @@ -49,6 +52,7 @@ class Crop extends StatefulWidget {
this.maximumScale = 2.0,
this.alwaysShowGrid = false,
this.onImageError,
this.rotation = 0,
}) : image = AssetImage(assetName, bundle: bundle, package: package),
super(key: key);

Expand All @@ -72,6 +76,7 @@ class CropState extends State<Crop> with TickerProviderStateMixin, Drag {
Offset _lastFocalPoint = Offset.zero;
_CropAction _action = _CropAction.none;
_CropHandleSide _handle = _CropHandleSide.none;
double rotation = 0;

late double _startScale;
late Rect _startView;
Expand Down Expand Up @@ -111,6 +116,7 @@ class CropState extends State<Crop> with TickerProviderStateMixin, Drag {
)..addListener(() => setState(() {}));
_settleController = AnimationController(vsync: this)
..addListener(_settleAnimationChanged);
rotation = widget.rotation;
}

@override
Expand All @@ -136,6 +142,7 @@ class CropState extends State<Crop> with TickerProviderStateMixin, Drag {
void didUpdateWidget(Crop oldWidget) {
super.didUpdateWidget(oldWidget);

rotation = widget.rotation;
if (widget.image != oldWidget.image) {
_getImage();
} else if (widget.aspectRatio != oldWidget.aspectRatio) {
Expand Down Expand Up @@ -184,15 +191,18 @@ class CropState extends State<Crop> with TickerProviderStateMixin, Drag {
onScaleStart: _isEnabled ? _handleScaleStart : null,
onScaleUpdate: _isEnabled ? _handleScaleUpdate : null,
onScaleEnd: _isEnabled ? _handleScaleEnd : null,
child: CustomPaint(
painter: _CropPainter(
image: _image,
ratio: _ratio,
view: _view,
area: _area,
scale: _scale,
active: _activeController.value,
child: Transform.rotate(
child: CustomPaint(
painter: _CropPainter(
image: _image,
ratio: _ratio,
view: _view,
area: _area,
scale: _scale,
active: _activeController.value,
),
),
angle: rotation,
),
),
),
Expand Down