Skip to content

Commit

Permalink
chore: enable to pass paintKey from parent
Browse files Browse the repository at this point in the history
When we need a small-sized image drawn by the user, repaintKey will be
usefull. Users can get a small-sized image by using repaintKey on their
own.
  • Loading branch information
satoyan committed Oct 21, 2024
1 parent 4243df7 commit 7221bf3
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions lib/src/_paint_over_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ImagePainter extends StatefulWidget {
this.onClear,
this.quarterTurns,
this.contentPadding,
this.repaintKey,
}) : super(key: key);

///Constructor for loading image from network url.
Expand Down Expand Up @@ -85,6 +86,7 @@ class ImagePainter extends StatefulWidget {
VoidCallback? onClear,
int? quarterTurns,
EdgeInsets? contentPadding,
GlobalKey? repaintKey,
}) {
return ImagePainter._(
key: key,
Expand Down Expand Up @@ -115,6 +117,7 @@ class ImagePainter extends StatefulWidget {
onClear: onClear,
quarterTurns: quarterTurns,
contentPadding: contentPadding,
repaintKey: repaintKey,
);
}

Expand Down Expand Up @@ -148,6 +151,7 @@ class ImagePainter extends StatefulWidget {
VoidCallback? onClear,
int? quarterTurns,
EdgeInsets? contentPadding,
GlobalKey? repaintKey,
}) {
return ImagePainter._(
controller: controller,
Expand Down Expand Up @@ -178,6 +182,7 @@ class ImagePainter extends StatefulWidget {
onClear: onClear,
quarterTurns: quarterTurns,
contentPadding: contentPadding,
repaintKey: repaintKey,
);
}

Expand Down Expand Up @@ -211,6 +216,7 @@ class ImagePainter extends StatefulWidget {
VoidCallback? onClear,
int? quarterTurns,
EdgeInsets? contentPadding,
GlobalKey? repaintKey,
}) {
return ImagePainter._(
controller: controller,
Expand Down Expand Up @@ -241,6 +247,7 @@ class ImagePainter extends StatefulWidget {
onClear: onClear,
quarterTurns: quarterTurns,
contentPadding: contentPadding,
repaintKey: repaintKey,
);
}

Expand Down Expand Up @@ -274,6 +281,7 @@ class ImagePainter extends StatefulWidget {
VoidCallback? onClear,
int? quarterTurns,
EdgeInsets? contentPadding,
GlobalKey? repaintKey,
}) {
return ImagePainter._(
controller: controller,
Expand Down Expand Up @@ -304,6 +312,7 @@ class ImagePainter extends StatefulWidget {
onClear: onClear,
quarterTurns: quarterTurns,
contentPadding: contentPadding,
repaintKey: repaintKey,
);
}

Expand Down Expand Up @@ -334,6 +343,7 @@ class ImagePainter extends StatefulWidget {
VoidCallback? onRedo,
VoidCallback? onClear,
EdgeInsets? contentPadding,
GlobalKey? repaintKey,
}) {
return ImagePainter._(
controller: controller,
Expand Down Expand Up @@ -363,6 +373,7 @@ class ImagePainter extends StatefulWidget {
onRedo: onRedo,
onClear: onClear,
contentPadding: contentPadding,
repaintKey: repaintKey ?? GlobalKey(),
);
}

Expand Down Expand Up @@ -453,13 +464,14 @@ class ImagePainter extends StatefulWidget {
// Padding for image content
final EdgeInsets? contentPadding;

final GlobalKey? repaintKey;

@override
ImagePainterState createState() => ImagePainterState();
}

///
class ImagePainterState extends State<ImagePainter> {
final _repaintKey = GlobalKey();
ui.Image? _image;
late final ImagePainterController _controller;
late final ValueNotifier<bool> _isLoaded;
Expand Down Expand Up @@ -604,30 +616,33 @@ class ImagePainterState extends State<ImagePainter> {
Expanded(
child: Padding(
padding: widget.contentPadding ?? EdgeInsets.zero,
child: RotatedBox(
quarterTurns: widget.quarterTurns ?? 0,
child: FittedBox(
alignment: FractionalOffset.center,
child: ClipRect(
child: AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return InteractiveViewer(
transformationController: _transformationController,
maxScale: 2.4,
minScale: 1,
panEnabled: _controller.mode == PaintMode.none,
scaleEnabled: widget.isScalable!,
onInteractionUpdate: _scaleUpdateGesture,
onInteractionEnd: _scaleEndGesture,
child: CustomPaint(
size: imageSize,
willChange: true,
isComplex: true,
painter: DrawImage(controller: _controller),
),
);
},
child: RepaintBoundary(
key: widget.repaintKey,
child: RotatedBox(
quarterTurns: widget.quarterTurns ?? 0,
child: FittedBox(
alignment: FractionalOffset.center,
child: ClipRect(
child: AnimatedBuilder(
animation: _controller,
builder: (context, child) {
return InteractiveViewer(
transformationController: _transformationController,
maxScale: 2.4,
minScale: 1,
panEnabled: _controller.mode == PaintMode.none,
scaleEnabled: widget.isScalable!,
onInteractionUpdate: _scaleUpdateGesture,
onInteractionEnd: _scaleEndGesture,
child: CustomPaint(
size: imageSize,
willChange: true,
isComplex: true,
painter: DrawImage(controller: _controller),
),
);
},
),
),
),
),
Expand All @@ -647,7 +662,7 @@ class ImagePainterState extends State<ImagePainter> {
return Stack(
children: [
RepaintBoundary(
key: _repaintKey,
key: widget.repaintKey,
child: ClipRect(
child: Container(
width: widget.width ?? double.maxFinite,
Expand Down

0 comments on commit 7221bf3

Please sign in to comment.