From 7ec1c316961d533d5771a3486d57107446320c86 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Fri, 2 Feb 2024 19:37:13 -0300 Subject: [PATCH] add more attributes --- .../attributes/decoration/decoration_dto.dart | 2 +- .../image/decoration_image_dto.dart | 38 +++++++++++++++- .../image/decoration_image_util.dart | 37 ++++++++++++++++ .../decoration/decoration_dto_test.dart | 1 + .../image/decoration_image_dto_test.dart | 43 +++++++++++++++++-- 5 files changed, 115 insertions(+), 6 deletions(-) diff --git a/lib/src/attributes/decoration/decoration_dto.dart b/lib/src/attributes/decoration/decoration_dto.dart index 690e228c6..4a513a3bc 100644 --- a/lib/src/attributes/decoration/decoration_dto.dart +++ b/lib/src/attributes/decoration/decoration_dto.dart @@ -148,7 +148,7 @@ class BoxDecorationDto extends DecorationDto { boxShadow: boxShadow?.merge(other.boxShadow) ?? other.boxShadow, shape: other.shape ?? shape, backgroundBlendMode: other.backgroundBlendMode ?? backgroundBlendMode, - image: image?.merge(other.image), + image: image?.merge(other.image) ?? other.image, ); } diff --git a/lib/src/attributes/decoration/image/decoration_image_dto.dart b/lib/src/attributes/decoration/image/decoration_image_dto.dart index de236a22e..cdb5e78f4 100644 --- a/lib/src/attributes/decoration/image/decoration_image_dto.dart +++ b/lib/src/attributes/decoration/image/decoration_image_dto.dart @@ -6,10 +6,22 @@ class DecorationImageDto extends Dto with Mergeable { final ImageProvider? image; final BoxFit? fit; + final AlignmentGeometry? alignment; + final Rect? centerSlice; + final ImageRepeat? repeat; + final FilterQuality? filterQuality; + final bool? invertColors; + final bool? isAntiAlias; const DecorationImageDto({ - required this.image, + this.image, this.fit, + this.alignment, + this.centerSlice, + this.repeat, + this.filterQuality, + this.invertColors, + this.isAntiAlias, }); static DecorationImageDto? maybeFrom(DecorationImage? image) { @@ -17,6 +29,12 @@ class DecorationImageDto extends Dto return DecorationImageDto( image: image.image, fit: image.fit, + alignment: image.alignment, + centerSlice: image.centerSlice, + repeat: image.repeat, + filterQuality: image.filterQuality, + invertColors: image.invertColors, + isAntiAlias: image.isAntiAlias, ); } @@ -25,6 +43,12 @@ class DecorationImageDto extends Dto return DecorationImageDto( image: other?.image ?? image, fit: other?.fit ?? fit, + alignment: other?.alignment ?? alignment, + centerSlice: other?.centerSlice ?? centerSlice, + repeat: other?.repeat ?? repeat, + filterQuality: other?.filterQuality ?? filterQuality, + invertColors: other?.invertColors ?? invertColors, + isAntiAlias: other?.isAntiAlias ?? isAntiAlias, ); } @@ -32,6 +56,12 @@ class DecorationImageDto extends Dto List get props => [ image, fit, + alignment, + centerSlice, + repeat, + filterQuality, + invertColors, + isAntiAlias, ]; @override @@ -39,6 +69,12 @@ class DecorationImageDto extends Dto return DecorationImage( image: image ?? const AssetImage(''), fit: fit, + alignment: alignment ?? Alignment.center, + centerSlice: centerSlice, + repeat: repeat ?? ImageRepeat.noRepeat, + filterQuality: filterQuality ?? FilterQuality.low, + invertColors: invertColors ?? false, + isAntiAlias: isAntiAlias ?? false, ); } } diff --git a/lib/src/attributes/decoration/image/decoration_image_util.dart b/lib/src/attributes/decoration/image/decoration_image_util.dart index d98de60b5..34766b1f1 100644 --- a/lib/src/attributes/decoration/image/decoration_image_util.dart +++ b/lib/src/attributes/decoration/image/decoration_image_util.dart @@ -21,14 +21,51 @@ class DecorationImageUtility return BoxFitUtility((fit) => _only(fit: fit)); } + AlignmentUtility get alignment { + return AlignmentUtility((alignment) => _only(alignment: alignment)); + } + + RectUtility get centerSlice { + return RectUtility((centerSlice) => _only(centerSlice: centerSlice)); + } + + ImageRepeatUtility get repeat { + return ImageRepeatUtility((repeat) => _only(repeat: repeat)); + } + + FilterQualityUtility get filterQuality { + return FilterQualityUtility( + (filterQuality) => _only(filterQuality: filterQuality)); + } + + BoolUtility get invertColors { + return BoolUtility((invertColors) => _only(invertColors: invertColors)); + } + + BoolUtility get isAntiAlias { + return BoolUtility((isAntiAlias) => _only(isAntiAlias: isAntiAlias)); + } + T _only({ ImageProvider? image, BoxFit? fit, + AlignmentGeometry? alignment, + Rect? centerSlice, + ImageRepeat? repeat, + FilterQuality? filterQuality, + bool? invertColors, + bool? isAntiAlias, }) { return builder( DecorationImageDto( image: image, fit: fit, + alignment: alignment, + centerSlice: centerSlice, + repeat: repeat, + filterQuality: filterQuality, + invertColors: invertColors, + isAntiAlias: isAntiAlias, ), ); } diff --git a/test/src/attributes/decoration/decoration_dto_test.dart b/test/src/attributes/decoration/decoration_dto_test.dart index 68ba959a8..3d3aa1207 100644 --- a/test/src/attributes/decoration/decoration_dto_test.dart +++ b/test/src/attributes/decoration/decoration_dto_test.dart @@ -20,6 +20,7 @@ void main() { test('merge returns merged object correctly', () { final decoration1 = BoxDecorationDto(color: Colors.red.toDto()); final decoration2 = BoxDecorationDto(gradient: linearGradientDto); + final merged = decoration1.merge(decoration2); expect(merged.color, decoration1.color); expect(merged.gradient, decoration2.gradient); diff --git a/test/src/attributes/decoration/image/decoration_image_dto_test.dart b/test/src/attributes/decoration/image/decoration_image_dto_test.dart index c15894dca..f0f026364 100644 --- a/test/src/attributes/decoration/image/decoration_image_dto_test.dart +++ b/test/src/attributes/decoration/image/decoration_image_dto_test.dart @@ -8,25 +8,60 @@ void main() { const dto = DecorationImageDto( image: imageProvider, fit: BoxFit.cover, + alignment: Alignment.topLeft, + centerSlice: Rect.fromLTRB(10, 20, 30, 40), + repeat: ImageRepeat.repeat, + filterQuality: FilterQuality.high, + invertColors: true, + isAntiAlias: true, ); test('maybeFrom', () { - const decorationImage = - DecorationImage(image: imageProvider, fit: BoxFit.cover); + const decorationImage = DecorationImage( + image: imageProvider, + fit: BoxFit.cover, + alignment: Alignment.topLeft, + centerSlice: Rect.fromLTRB(10, 20, 30, 40), + repeat: ImageRepeat.repeat, + filterQuality: FilterQuality.high, + invertColors: true, + isAntiAlias: true, + ); final result = DecorationImageDto.maybeFrom(decorationImage); expect(result, isNotNull); expect(result!.image, equals(imageProvider)); expect(result.fit, equals(BoxFit.cover)); + expect(result.alignment, equals(Alignment.topLeft)); + expect(result.centerSlice, equals(const Rect.fromLTRB(10, 20, 30, 40))); + expect(result.repeat, equals(ImageRepeat.repeat)); + expect(result.filterQuality, equals(FilterQuality.high)); + expect(result.invertColors, equals(true)); + expect(result.isAntiAlias, equals(true)); }); test('merge', () { - const otherDto = - DecorationImageDto(image: imageProvider, fit: BoxFit.fill); + const otherDto = DecorationImageDto( + image: imageProvider, + fit: BoxFit.fill, + alignment: Alignment.bottomRight, + centerSlice: Rect.fromLTRB(50, 60, 70, 80), + repeat: ImageRepeat.repeatX, + filterQuality: FilterQuality.low, + invertColors: false, + isAntiAlias: false, + ); final mergedDto = dto.merge(otherDto); expect(mergedDto.image, equals(imageProvider)); expect(mergedDto.fit, equals(BoxFit.fill)); + expect(mergedDto.alignment, equals(Alignment.bottomRight)); + expect( + mergedDto.centerSlice, equals(const Rect.fromLTRB(50, 60, 70, 80))); + expect(mergedDto.repeat, equals(ImageRepeat.repeatX)); + expect(mergedDto.filterQuality, equals(FilterQuality.low)); + expect(mergedDto.invertColors, equals(false)); + expect(mergedDto.isAntiAlias, equals(false)); }); }); }