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

add initial aspect ratio #51

Open
wants to merge 1 commit into
base: main
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
20 changes: 17 additions & 3 deletions lib/src/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Crop extends StatelessWidget {
/// null, by default, means no fixed aspect ratio.
final double? aspectRatio;

/// initial aspect ration of cropping area.
/// you can change this ratio. it is not fixed!
final double? initialAspectRatio;

/// initial size of cropping area.
/// Set double value less than 1.0.
/// if initialSize is 1.0 (or null),
Expand Down Expand Up @@ -71,6 +75,7 @@ class Crop extends StatelessWidget {
required this.image,
required this.onCropped,
this.aspectRatio,
this.initialAspectRatio,
this.initialSize,
this.initialArea,
this.withCircleUi = false,
Expand All @@ -97,6 +102,7 @@ class Crop extends StatelessWidget {
image: image,
onCropped: onCropped,
aspectRatio: aspectRatio,
initialAspectRatio: initialAspectRatio,
initialSize: initialSize,
initialArea: initialArea,
withCircleUi: withCircleUi,
Expand All @@ -117,6 +123,7 @@ class _CropEditor extends StatefulWidget {
final Uint8List image;
final ValueChanged<Uint8List> onCropped;
final double? aspectRatio;
final double? initialAspectRatio;
final double? initialSize;
final Rect? initialArea;
final bool withCircleUi;
Expand All @@ -132,6 +139,7 @@ class _CropEditor extends StatefulWidget {
required this.image,
required this.onCropped,
this.aspectRatio,
this.initialAspectRatio,
this.initialSize,
this.initialArea,
this.withCircleUi = false,
Expand All @@ -154,6 +162,7 @@ class _CropEditorState extends State<_CropEditor> {
late Rect _imageRect;

double? _aspectRatio;
double? _initialAspectRatio;
bool _withCircleUi = false;
bool _isFitVertically = false;
Future<image.Image?>? _lastComputed;
Expand Down Expand Up @@ -239,18 +248,23 @@ class _CropEditorState extends State<_CropEditor> {

_imageRect = calculator.imageRect(screenSize, imageRatio);

_resizeWith(widget.aspectRatio, widget.initialArea);
_resizeWith(
widget.aspectRatio,
widget.initialArea,
initialAspectRatio: widget.initialAspectRatio,
);
}

/// resize cropping area with given aspect ratio.
void _resizeWith(double? aspectRatio, Rect? initialArea) {
void _resizeWith(double? aspectRatio, Rect? initialArea,
{double? initialAspectRatio}) {
_aspectRatio = _withCircleUi ? 1 : aspectRatio;

if (initialArea == null) {
rect = calculator.initialCropRect(
MediaQuery.of(context).size,
_imageRect,
_aspectRatio ?? 1,
_aspectRatio ?? initialAspectRatio ?? 1,
widget.initialSize ?? 1,
);
} else {
Expand Down