From ebd0015c5e9a658aa165753e5f2ccaaacaf14096 Mon Sep 17 00:00:00 2001 From: vinicios-cervantes Date: Wed, 5 Jan 2022 17:04:20 -0300 Subject: [PATCH] Added borderColor When borderColor is set, a border with the given color is added to the image exterior, making it better to see the image ending when the baseColor and the image background are similar --- example/lib/main.dart | 16 ++++++++++++++++ lib/src/crop.dart | 30 +++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index ccce96a..8f0f0b0 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -57,6 +57,7 @@ class _CropSampleState extends State { } var _isSumbnail = false; + var _useBorders = false; var _isCropping = false; var _isCircleUi = false; Uint8List? _croppedData; @@ -117,6 +118,7 @@ class _CropSampleState extends State { children: [ if (_imageDataList.isNotEmpty) Crop( + borderColor: _useBorders ? Colors.black : null, controller: _cropController, image: _imageDataList[_currentImage], onCropped: (croppedData) { @@ -143,6 +145,20 @@ class _CropSampleState extends State { ? const SizedBox.shrink() : const DotControl(), ), + Positioned( + right: 16, + bottom: 60, + child: GestureDetector( + onTap: () => setState(() => _useBorders = !_useBorders), + child: CircleAvatar( + backgroundColor: + _useBorders ? Colors.blue.shade50 : Colors.blue, + child: Center( + child: Icon(Icons.border_outer_rounded), + ), + ), + ), + ), Positioned( right: 16, bottom: 16, diff --git a/lib/src/crop.dart b/lib/src/crop.dart index aff19ca..453dc4f 100644 --- a/lib/src/crop.dart +++ b/lib/src/crop.dart @@ -66,6 +66,12 @@ class Crop extends StatelessWidget { /// If default dot Widget with different color is needed, [DotControl] is available. final CornerDotBuilder? cornerDotBuilder; + /// [Color] of the image border + /// When [borderColor] is set, a border with the given color is added to the + /// image exterior, making it better to see the image ending when + /// the [baseColor] and the image background are similar + final Color? borderColor; + const Crop({ Key? key, required this.image, @@ -80,6 +86,7 @@ class Crop extends StatelessWidget { this.maskColor, this.baseColor = Colors.white, this.cornerDotBuilder, + this.borderColor, }) : assert((initialSize ?? 1.0) <= 1.0, 'initialSize must be less than 1.0, or null meaning not specified.'), super(key: key); @@ -106,6 +113,7 @@ class Crop extends StatelessWidget { maskColor: maskColor, baseColor: baseColor, cornerDotBuilder: cornerDotBuilder, + borderColor: borderColor, ), ); }, @@ -126,6 +134,7 @@ class _CropEditor extends StatefulWidget { final Color? maskColor; final Color baseColor; final CornerDotBuilder? cornerDotBuilder; + final Color? borderColor; const _CropEditor({ Key? key, @@ -141,6 +150,7 @@ class _CropEditor extends StatefulWidget { this.maskColor, required this.baseColor, this.cornerDotBuilder, + this.borderColor, }) : super(key: key); @override @@ -306,9 +316,23 @@ class _CropEditorState extends State<_CropEditor> { color: widget.baseColor, width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, - child: Image.memory( - widget.image, - fit: _isFitVertically ? BoxFit.fitHeight : BoxFit.fitWidth, + child: Flex( + direction: _isFitVertically ? Axis.vertical : Axis.horizontal, + children: [ + Expanded( + child: Container( + foregroundDecoration: (widget.borderColor != null && widget.maskColor == null) + ? BoxDecoration( + border: Border.all(color: widget.borderColor!, width: 2), + ) + : null, + child: Image.memory( + widget.image, + fit: _isFitVertically ? BoxFit.fitHeight : BoxFit.fitWidth, + ), + ), + ), + ], ), ), IgnorePointer(