Skip to content

Commit

Permalink
add: placeholderWidget parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
LeGoffMael committed Dec 16, 2022
1 parent 5809167 commit 7ea153c
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions lib/src/crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ class Crop extends StatefulWidget {
final bool alwaysShowGrid;
final ImageErrorListener? onImageError;

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

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

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

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

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

_getImage();
}

Expand All @@ -138,6 +143,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 +162,13 @@ class CropState extends State<Crop> with TickerProviderStateMixin, Drag {
}
}

void _getImage({bool force = false}) {
void _getImage() {
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 +192,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

0 comments on commit 7ea153c

Please sign in to comment.