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: placeholderWidget parameter #97

Open
wants to merge 2 commits 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
49 changes: 36 additions & 13 deletions lib/src/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ class Crop extends StatefulWidget {
final bool alwaysShowGrid;
final ImageErrorListener? onImageError;

/// Specifies [placeholderWidget] to display a [Widget] while the image is loading
final Widget? placeholderWidget;

/// Function called when the image or the view is recomputed
final Function(bool isReady)? onLoading;

const Crop({
Key? key,
required this.image,
this.aspectRatio,
this.maximumScale = 2.0,
this.alwaysShowGrid = false,
this.onImageError,
this.placeholderWidget,
this.onLoading,
}) : super(key: key);

Crop.file(
Expand All @@ -38,6 +46,8 @@ class Crop extends StatefulWidget {
this.maximumScale = 2.0,
this.alwaysShowGrid = false,
this.onImageError,
this.placeholderWidget,
this.onLoading,
}) : image = FileImage(file, scale: scale),
super(key: key);

Expand All @@ -50,6 +60,8 @@ class Crop extends StatefulWidget {
this.maximumScale = 2.0,
this.alwaysShowGrid = false,
this.onImageError,
this.placeholderWidget,
this.onLoading,
}) : image = AssetImage(assetName, bundle: bundle, package: package),
super(key: key);

Expand Down Expand Up @@ -129,7 +141,6 @@ class CropState extends State<Crop> with TickerProviderStateMixin, Drag {
@override
void didChangeDependencies() {
super.didChangeDependencies();

_getImage();
}

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

if (widget.image != oldWidget.image) {
_image = null;
_getImage();
} else if (widget.aspectRatio != oldWidget.aspectRatio) {
_area = _calculateDefaultArea(
Expand All @@ -156,12 +168,20 @@ class CropState extends State<Crop> with TickerProviderStateMixin, Drag {
}
}

void _getImage({bool force = false}) {
void _onLoading(bool isLoading) {
if (widget.onLoading != null) {
widget.onLoading!(isLoading);
}
}

void _getImage() {
_onLoading(false);
widget.image.evict();
final oldImageStream = _imageStream;
final newImageStream =
widget.image.resolve(createLocalImageConfiguration(context));
_imageStream = newImageStream;
if (newImageStream.key != oldImageStream?.key || force) {
if (newImageStream.key != oldImageStream?.key) {
final oldImageListener = _imageListener;
if (oldImageListener != null) {
oldImageStream?.removeListener(oldImageListener);
Expand All @@ -185,16 +205,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: _image == null && widget.placeholderWidget != null
? widget.placeholderWidget
: CustomPaint(
painter: _CropPainter(
image: _image,
ratio: _ratio,
view: _view,
area: _area,
scale: _scale,
active: _activeController.value,
),
),
),
),
);
Expand Down Expand Up @@ -326,6 +348,7 @@ class CropState extends State<Crop> with TickerProviderStateMixin, Drag {
viewWidth,
viewHeight,
);
_onLoading(true);
});
});

Expand Down