From ad349ba0c42f7ced9f109eb422e903d76c548f23 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Fri, 2 Feb 2024 23:25:51 -0300 Subject: [PATCH] add new attribute to foregroundDecoration --- lib/src/specs/container/box_attribute.dart | 7 ++++ lib/src/specs/container/box_spec.dart | 11 +++++++ lib/src/specs/container/box_util.dart | 9 ++++++ lib/src/specs/container/box_widget.dart | 2 ++ test/src/specs/container/box_spec_test.dart | 20 ++++++++++++ test/src/specs/container/box_util_test.dart | 36 +++++++++++++++++++++ 6 files changed, 85 insertions(+) diff --git a/lib/src/specs/container/box_attribute.dart b/lib/src/specs/container/box_attribute.dart index cbbc513d2..09153f7e7 100644 --- a/lib/src/specs/container/box_attribute.dart +++ b/lib/src/specs/container/box_attribute.dart @@ -13,6 +13,7 @@ class BoxSpecAttribute extends SpecAttribute { final SpacingDto? margin; final BoxConstraintsDto? constraints; final DecorationDto? decoration; + final DecorationDto? foregroundDecoration; final Matrix4? transform; final Clip? clipBehavior; final double? width; @@ -24,6 +25,7 @@ class BoxSpecAttribute extends SpecAttribute { this.margin, this.constraints, this.decoration, + this.foregroundDecoration, this.transform, this.clipBehavior, this.width, @@ -38,6 +40,7 @@ class BoxSpecAttribute extends SpecAttribute { margin: margin?.resolve(mix), constraints: constraints?.resolve(mix), decoration: decoration?.resolve(mix), + foregroundDecoration: foregroundDecoration?.resolve(mix), transform: transform, clipBehavior: clipBehavior, width: width, @@ -55,6 +58,9 @@ class BoxSpecAttribute extends SpecAttribute { margin: margin?.merge(other.margin) ?? other.margin, constraints: constraints?.merge(other.constraints) ?? other.constraints, decoration: decoration?.merge(other.decoration) ?? other.decoration, + foregroundDecoration: + foregroundDecoration?.merge(other.foregroundDecoration) ?? + other.foregroundDecoration, transform: other.transform ?? transform, clipBehavior: other.clipBehavior ?? clipBehavior, width: other.width ?? width, @@ -69,6 +75,7 @@ class BoxSpecAttribute extends SpecAttribute { margin, constraints, decoration, + foregroundDecoration, transform, clipBehavior, width, diff --git a/lib/src/specs/container/box_spec.dart b/lib/src/specs/container/box_spec.dart index 8cdcc17d1..00c9c7047 100644 --- a/lib/src/specs/container/box_spec.dart +++ b/lib/src/specs/container/box_spec.dart @@ -13,6 +13,7 @@ class BoxSpec extends Spec { final EdgeInsetsGeometry? margin; final BoxConstraints? constraints; final Decoration? decoration; + final Decoration? foregroundDecoration; final Matrix4? transform; final Clip? clipBehavior; final double? width; @@ -24,6 +25,7 @@ class BoxSpec extends Spec { required this.margin, required this.constraints, required this.decoration, + required this.foregroundDecoration, required this.transform, required this.clipBehavior, required this.width, @@ -36,6 +38,7 @@ class BoxSpec extends Spec { margin = null, constraints = null, decoration = null, + foregroundDecoration = null, transform = null, width = null, height = null, @@ -53,6 +56,7 @@ class BoxSpec extends Spec { EdgeInsetsGeometry? margin, BoxConstraints? constraints, Decoration? decoration, + Decoration? foregroundDecoration, double? width, double? height, Matrix4? transform, @@ -65,6 +69,7 @@ class BoxSpec extends Spec { margin: margin ?? this.margin, constraints: constraints ?? this.constraints, decoration: decoration ?? this.decoration, + foregroundDecoration: foregroundDecoration ?? this.foregroundDecoration, transform: transform ?? this.transform, clipBehavior: clipBehavior ?? this.clipBehavior, width: width ?? this.width, @@ -80,6 +85,11 @@ class BoxSpec extends Spec { margin: EdgeInsetsGeometry.lerp(margin, other.margin, t), constraints: BoxConstraints.lerp(constraints, other.constraints, t), decoration: Decoration.lerp(decoration, other.decoration, t), + foregroundDecoration: Decoration.lerp( + foregroundDecoration, + other.foregroundDecoration, + t, + ), transform: Matrix4Tween(begin: transform, end: other.transform).lerp(t), clipBehavior: lerpSnap(clipBehavior, other.clipBehavior, t), width: lerpDouble(width, other.width, t), @@ -96,6 +106,7 @@ class BoxSpec extends Spec { margin, constraints, decoration, + foregroundDecoration, transform, clipBehavior, ]; diff --git a/lib/src/specs/container/box_util.dart b/lib/src/specs/container/box_util.dart index 1a804735f..74830c285 100644 --- a/lib/src/specs/container/box_util.dart +++ b/lib/src/specs/container/box_util.dart @@ -295,6 +295,13 @@ class BoxSpecUtility extends SpecUtility { ); } + BoxDecorationUtility get foregroundDecoration { + return BoxDecorationUtility( + (foregroundDecoration) => + only(foregroundDecoration: foregroundDecoration), + ); + } + AlignmentUtility get alignment { return AlignmentUtility((alignment) => only(alignment: alignment)); } @@ -364,6 +371,7 @@ class BoxSpecUtility extends SpecUtility { SpacingDto? padding, SpacingDto? margin, DecorationDto? decoration, + DecorationDto? foregroundDecoration, BoxConstraintsDto? constraints, double? width, double? height, @@ -376,6 +384,7 @@ class BoxSpecUtility extends SpecUtility { margin: margin, constraints: constraints, decoration: decoration, + foregroundDecoration: foregroundDecoration, transform: transform, clipBehavior: clipBehavior, width: width, diff --git a/lib/src/specs/container/box_widget.dart b/lib/src/specs/container/box_widget.dart index 759cbd6b7..75ea6d2b4 100644 --- a/lib/src/specs/container/box_widget.dart +++ b/lib/src/specs/container/box_widget.dart @@ -119,6 +119,7 @@ class MixedBox extends StatelessWidget { alignment: spec.alignment, padding: spec.padding, decoration: spec.decoration, + foregroundDecoration: spec.foregroundDecoration, width: spec.width, height: spec.height, constraints: spec.constraints, @@ -225,6 +226,7 @@ class AnimatedMixedBox extends StatelessWidget { width: spec.width, height: spec.height, constraints: spec.constraints, + foregroundDecoration: spec.foregroundDecoration, margin: spec.margin, transform: spec.transform, clipBehavior: spec.clipBehavior ?? Clip.none, diff --git a/test/src/specs/container/box_spec_test.dart b/test/src/specs/container/box_spec_test.dart index 83aa588c1..172c9f91e 100644 --- a/test/src/specs/container/box_spec_test.dart +++ b/test/src/specs/container/box_spec_test.dart @@ -59,6 +59,7 @@ void main() { margin: const EdgeInsets.only(top: 8.0, bottom: 8.0), constraints: const BoxConstraints(maxWidth: 300.0, minHeight: 200.0), decoration: const BoxDecoration(color: Colors.blue), + foregroundDecoration: const BoxDecoration(color: Colors.purple), transform: Matrix4.translationValues(10.0, 10.0, 0.0), clipBehavior: Clip.antiAlias, ); @@ -71,6 +72,8 @@ void main() { expect(copiedSpec.constraints, const BoxConstraints(maxWidth: 300.0, minHeight: 200.0)); expect(copiedSpec.decoration, const BoxDecoration(color: Colors.blue)); + expect(copiedSpec.foregroundDecoration, + const BoxDecoration(color: Colors.purple)); expect(copiedSpec.transform, Matrix4.translationValues(10.0, 10.0, 0.0)); expect(copiedSpec.clipBehavior, Clip.antiAlias); @@ -87,6 +90,7 @@ void main() { margin: const EdgeInsets.only(top: 4.0), constraints: const BoxConstraints(maxWidth: 200.0), decoration: const BoxDecoration(color: Colors.red), + foregroundDecoration: const BoxDecoration(color: Colors.blue), transform: Matrix4.identity(), clipBehavior: Clip.none, ); @@ -99,6 +103,7 @@ void main() { margin: const EdgeInsets.only(top: 8.0), constraints: const BoxConstraints(maxWidth: 400.0), decoration: const BoxDecoration(color: Colors.blue), + foregroundDecoration: const BoxDecoration(color: Colors.red), transform: Matrix4.rotationZ(0.5), clipBehavior: Clip.antiAlias, ); @@ -127,6 +132,13 @@ void main() { end: const BoxDecoration(color: Colors.blue)) .lerp(t)); + expect( + lerpedSpec.foregroundDecoration, + DecorationTween( + begin: const BoxDecoration(color: Colors.blue), + end: const BoxDecoration(color: Colors.red)) + .lerp(t)); + expect(lerpedSpec.width, lerpDouble(300, 400, t)); expect(lerpedSpec.height, lerpDouble(200, 300, t)); @@ -147,6 +159,7 @@ void main() { margin: const EdgeInsets.only(top: 4.0), constraints: const BoxConstraints(maxWidth: 200.0), decoration: const BoxDecoration(color: Colors.red), + foregroundDecoration: const BoxDecoration(color: Colors.blue), transform: Matrix4.identity(), clipBehavior: Clip.none, ); @@ -159,6 +172,7 @@ void main() { margin: const EdgeInsets.only(top: 4.0), constraints: const BoxConstraints(maxWidth: 200.0), decoration: const BoxDecoration(color: Colors.red), + foregroundDecoration: const BoxDecoration(color: Colors.blue), transform: Matrix4.identity(), clipBehavior: Clip.none, ); @@ -173,6 +187,8 @@ void main() { clipBehavior: Clip.antiAlias, constraints: const BoxConstraintsDto(maxHeight: 100), decoration: const BoxDecorationDto(color: ColorDto(Colors.blue)), + foregroundDecoration: + const BoxDecorationDto(color: ColorDto(Colors.blue)), height: 100, margin: const SpacingDto.only( bottom: 10, @@ -192,6 +208,8 @@ void main() { clipBehavior: Clip.antiAliasWithSaveLayer, constraints: const BoxConstraintsDto(maxHeight: 200), decoration: const BoxDecorationDto(color: ColorDto(Colors.red)), + foregroundDecoration: + const BoxDecorationDto(color: ColorDto(Colors.amber)), height: 200, margin: const SpacingDto.only( bottom: 20, @@ -213,6 +231,8 @@ void main() { const BoxConstraintsDto(maxHeight: 200)); expect(mergedBoxSpecAttribute.decoration, const BoxDecorationDto(color: ColorDto(Colors.red))); + expect(mergedBoxSpecAttribute.foregroundDecoration, + const BoxDecorationDto(color: ColorDto(Colors.amber))); expect(mergedBoxSpecAttribute.height, 200); expect( mergedBoxSpecAttribute.margin, diff --git a/test/src/specs/container/box_util_test.dart b/test/src/specs/container/box_util_test.dart index 81956cd5a..b2baffec7 100644 --- a/test/src/specs/container/box_util_test.dart +++ b/test/src/specs/container/box_util_test.dart @@ -101,6 +101,42 @@ void main() { expect(container.width, 10); }); + + test('decoration() returns correct instance', () { + final container = boxUtility.decoration( + color: Colors.amber, + borderRadius: BorderRadius.circular(10), + ); + + expect(container.decoration!.color, const ColorDto(Colors.amber)); + + final decorationDTO = container.decoration as BoxDecorationDto; + expect( + decorationDTO.borderRadius, + BorderRadiusGeometryDto.from(BorderRadius.circular(10)), + ); + }); + + test('foregroundDecoration() returns correct instance', () { + final container = boxUtility.foregroundDecoration( + color: Colors.amber, + borderRadius: BorderRadius.circular(10), + ); + + expect( + container.foregroundDecoration!.color, + const ColorDto(Colors.amber), + reason: 'The color is not correct', + ); + + final foregroundDecorationDTO = + container.foregroundDecoration as BoxDecorationDto; + expect( + foregroundDecorationDTO.borderRadius, + BorderRadiusGeometryDto.from(BorderRadius.circular(10)), + reason: 'The BorderRadius is not correct', + ); + }); }, ); }