From 7221bf3eeaa18c0122414982718e8adcc724999c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=95=E3=81=A8=E3=82=84=EF=BD=8E?= Date: Mon, 21 Oct 2024 18:46:22 +0900 Subject: [PATCH] chore: enable to pass paintKey from parent 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. --- lib/src/_paint_over_image.dart | 67 +++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/lib/src/_paint_over_image.dart b/lib/src/_paint_over_image.dart index f7a6abb..0c7bd2d 100644 --- a/lib/src/_paint_over_image.dart +++ b/lib/src/_paint_over_image.dart @@ -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. @@ -85,6 +86,7 @@ class ImagePainter extends StatefulWidget { VoidCallback? onClear, int? quarterTurns, EdgeInsets? contentPadding, + GlobalKey? repaintKey, }) { return ImagePainter._( key: key, @@ -115,6 +117,7 @@ class ImagePainter extends StatefulWidget { onClear: onClear, quarterTurns: quarterTurns, contentPadding: contentPadding, + repaintKey: repaintKey, ); } @@ -148,6 +151,7 @@ class ImagePainter extends StatefulWidget { VoidCallback? onClear, int? quarterTurns, EdgeInsets? contentPadding, + GlobalKey? repaintKey, }) { return ImagePainter._( controller: controller, @@ -178,6 +182,7 @@ class ImagePainter extends StatefulWidget { onClear: onClear, quarterTurns: quarterTurns, contentPadding: contentPadding, + repaintKey: repaintKey, ); } @@ -211,6 +216,7 @@ class ImagePainter extends StatefulWidget { VoidCallback? onClear, int? quarterTurns, EdgeInsets? contentPadding, + GlobalKey? repaintKey, }) { return ImagePainter._( controller: controller, @@ -241,6 +247,7 @@ class ImagePainter extends StatefulWidget { onClear: onClear, quarterTurns: quarterTurns, contentPadding: contentPadding, + repaintKey: repaintKey, ); } @@ -274,6 +281,7 @@ class ImagePainter extends StatefulWidget { VoidCallback? onClear, int? quarterTurns, EdgeInsets? contentPadding, + GlobalKey? repaintKey, }) { return ImagePainter._( controller: controller, @@ -304,6 +312,7 @@ class ImagePainter extends StatefulWidget { onClear: onClear, quarterTurns: quarterTurns, contentPadding: contentPadding, + repaintKey: repaintKey, ); } @@ -334,6 +343,7 @@ class ImagePainter extends StatefulWidget { VoidCallback? onRedo, VoidCallback? onClear, EdgeInsets? contentPadding, + GlobalKey? repaintKey, }) { return ImagePainter._( controller: controller, @@ -363,6 +373,7 @@ class ImagePainter extends StatefulWidget { onRedo: onRedo, onClear: onClear, contentPadding: contentPadding, + repaintKey: repaintKey ?? GlobalKey(), ); } @@ -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 { - final _repaintKey = GlobalKey(); ui.Image? _image; late final ImagePainterController _controller; late final ValueNotifier _isLoaded; @@ -604,30 +616,33 @@ class ImagePainterState extends State { 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), + ), + ); + }, + ), ), ), ), @@ -647,7 +662,7 @@ class ImagePainterState extends State { return Stack( children: [ RepaintBoundary( - key: _repaintKey, + key: widget.repaintKey, child: ClipRect( child: Container( width: widget.width ?? double.maxFinite,