From 7e8287dba000f80d86ba5e2248cf3ff158f027b3 Mon Sep 17 00:00:00 2001 From: Leo Farias Date: Fri, 2 Feb 2024 09:54:07 -0500 Subject: [PATCH 1/4] Fixes merge behavior --- lib/src/attributes/border/border_dto.dart | 16 +++++----- .../attributes/border/border_dto_test.dart | 31 +++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/lib/src/attributes/border/border_dto.dart b/lib/src/attributes/border/border_dto.dart index ddde3b109..eae4b470a 100644 --- a/lib/src/attributes/border/border_dto.dart +++ b/lib/src/attributes/border/border_dto.dart @@ -81,19 +81,19 @@ class BoxBorderDto extends Dto with Mergeable { assert(type != null, 'Cannot merge Border with BoxBorderDirectional'); if (type == Border) { return BoxBorderDto( - top: other.top ?? top, - bottom: other.bottom ?? bottom, - left: other.left ?? left, - right: other.right ?? right, + top: top?.merge(other.top) ?? other.top, + bottom: bottom?.merge(other.bottom) ?? other.bottom, + left: left?.merge(other.left) ?? other.left, + right: right?.merge(other.right) ?? other.right, ); } if (type == BorderDirectional) { return BoxBorderDto( - top: other.top ?? top, - bottom: other.bottom ?? bottom, - start: other.start ?? start, - end: other.end ?? end, + top: top?.merge(other.top) ?? other.top, + bottom: bottom?.merge(other.bottom) ?? other.bottom, + start: start?.merge(other.start) ?? other.start, + end: end?.merge(other.end) ?? other.end, ); } diff --git a/test/src/attributes/border/border_dto_test.dart b/test/src/attributes/border/border_dto_test.dart index aa2e75913..81bed49ec 100644 --- a/test/src/attributes/border/border_dto_test.dart +++ b/test/src/attributes/border/border_dto_test.dart @@ -82,6 +82,37 @@ void main() { expect(resolvedBorder.start, const BorderSide(width: 15.0)); expect(resolvedBorder.end, const BorderSide(width: 20.0)); }); + + // merge + test('merge() Border', () { + const borderDto1 = BoxBorderDto( + top: BorderSideDto(width: 1.0, color: ColorDto(Colors.red)), + bottom: BorderSideDto(width: 1.0, color: ColorDto(Colors.red)), + left: BorderSideDto(width: 1.0, color: ColorDto(Colors.red)), + right: BorderSideDto(width: 1.0, color: ColorDto(Colors.red)), + ); + + const borderDto2 = BoxBorderDto( + top: BorderSideDto(width: 2.0), + bottom: BorderSideDto(width: 2.0), + left: BorderSideDto(width: 2.0), + right: BorderSideDto(width: 2.0), + ); + + final merged = borderDto1.merge(borderDto2); + + expect(merged.top?.width, 2.0); + expect(merged.top?.color, const ColorDto(Colors.red)); + + expect(merged.bottom?.width, 2.0); + expect(merged.bottom?.color, const ColorDto(Colors.red)); + + expect(merged.left?.width, 2.0); + expect(merged.left?.color, const ColorDto(Colors.red)); + + expect(merged.right?.width, 2.0); + expect(merged.right?.color, const ColorDto(Colors.red)); + }); }); // BorderSideDto tests From aa4962aa974cdfc008d9e3adf0b77409a6fb61a5 Mon Sep 17 00:00:00 2001 From: Leo Farias Date: Fri, 2 Feb 2024 09:54:14 -0500 Subject: [PATCH 2/4] Updated utilities --- lib/src/attributes/border/border_util.dart | 61 ++++++++++++++-------- lib/src/attributes/color/color_util.dart | 26 ++++----- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/lib/src/attributes/border/border_util.dart b/lib/src/attributes/border/border_util.dart index 0b37e37f7..a86ce6861 100644 --- a/lib/src/attributes/border/border_util.dart +++ b/lib/src/attributes/border/border_util.dart @@ -37,12 +37,14 @@ class BorderSideUtility BorderStyle? style, double? strokeAlign, }) { - return builder(BorderSideDto( - color: color, - strokeAlign: strokeAlign, - style: style, - width: width, - )); + return builder( + BorderSideDto( + color: color, + strokeAlign: strokeAlign, + style: style, + width: width, + ), + ); } /// Returns a [ColorUtility] to manipulate [Color] of the [BorderSideDto] @@ -52,12 +54,16 @@ class BorderSideUtility BorderStyleUtility get style => BorderStyleUtility((style) => _only(style: style)); + T as(BorderSide side) => builder(BorderSideDto.from(side)); + /// Sets the width of the [BorderSideDto] T width(double width) => call(width: width); /// Sets the stroke align of the [BorderSideDto] T strokeAlign(double strokeAlign) => call(strokeAlign: strokeAlign); + T none() => as(BorderSide.none); + /// Creates a [BorderSideDto] with the provided parameters and calls the [builder] function. T call({ Color? color, @@ -80,6 +86,7 @@ class BorderUtility extends DtoUtility { /// Constructor for creating an instance of the class. const BorderUtility(super.builder) : super(valueToDto: BoxBorderDto.from); + BoxBorderDto _symmetric({ BorderSideDto? vertical, BorderSideDto? horizontal, @@ -96,17 +103,6 @@ class BorderUtility return BoxBorderDto(top: side, bottom: side, left: side, right: side); } - T _only({ - BorderSideDto? top, - BorderSideDto? bottom, - BorderSideDto? left, - BorderSideDto? right, - }) { - return builder( - BoxBorderDto(top: top, bottom: bottom, left: left, right: right), - ); - } - BorderDirectionalUtility get _directional => BorderDirectionalUtility((border) => builder(border)); @@ -123,22 +119,22 @@ class BorderUtility /// Method to set the border on the bottom side. BorderSideUtility get bottom { - return BorderSideUtility((side) => _only(bottom: side)); + return BorderSideUtility((side) => only(bottom: side)); } /// Method to set the border on the top side. BorderSideUtility get top { - return BorderSideUtility((side) => _only(top: side)); + return BorderSideUtility((side) => only(top: side)); } /// Method to set the border on the left side. BorderSideUtility get left { - return BorderSideUtility((side) => _only(left: side)); + return BorderSideUtility((side) => only(left: side)); } /// Method to set the border on the right side. BorderSideUtility get right { - return BorderSideUtility((side) => _only(right: side)); + return BorderSideUtility((side) => only(right: side)); } /// Method to set the borders on the vertical sides. @@ -153,6 +149,29 @@ class BorderUtility ); } + /// Returns a [ColorUtility] to manipulate [Color] of the [BorderSideDto] + ColorUtility get color => all.color; + + BorderStyleUtility get style => all.style; + + T width(double width) => all.width(width); + + T strokeAlign(double strokeAlign) => all.strokeAlign(strokeAlign); + + T none() => all.none(); + + /// Method to set the border individually on each side. + T only({ + BorderSideDto? top, + BorderSideDto? bottom, + BorderSideDto? left, + BorderSideDto? right, + }) { + return builder( + BoxBorderDto(top: top, bottom: bottom, left: left, right: right), + ); + } + /// Creates a [BoxBorderDto] with the provided parameters and calls the [builder] function. T call({ Color? color, diff --git a/lib/src/attributes/color/color_util.dart b/lib/src/attributes/color/color_util.dart index cbafd6899..a42f79ad6 100644 --- a/lib/src/attributes/color/color_util.dart +++ b/lib/src/attributes/color/color_util.dart @@ -19,19 +19,6 @@ class ColorUtility T _directive(ColorDirective directive) => builder(ColorDto.directive(directive)); - T withOpacity(double opacity) => _directive(OpacityColorDirective(opacity)); - T withAlpha(int alpha) => _directive(AlphaColorDirective(alpha)); - T darken(int percentage) => _directive(DarkenColorDirective(percentage)); - T lighten(int percentage) => _directive(LightenColorDirective(percentage)); - T saturate(int percentage) => _directive(SaturateColorDirective(percentage)); - T desaturate(int percentage) => - _directive(DesaturateColorDirective(percentage)); - T tint(int percentage) => _directive(TintColorDirective(percentage)); - T shade(int percentage) => _directive(ShadeColorDirective(percentage)); - T brighten(int percentage) => _directive(BrightenColorDirective(percentage)); - - T of(ColorToken ref) => _buildColor(ref()); - MaterialColorUtility get red => MaterialColorUtility(builder, Colors.red); MaterialColorUtility get pink => MaterialColorUtility(builder, Colors.pink); @@ -101,6 +88,19 @@ class ColorUtility MaterialAccentColorUtility(builder, Colors.orangeAccent); MaterialAccentColorUtility get deepOrangeAccent => MaterialAccentColorUtility(builder, Colors.deepOrangeAccent); + T withOpacity(double opacity) => _directive(OpacityColorDirective(opacity)); + T withAlpha(int alpha) => _directive(AlphaColorDirective(alpha)); + T darken(int percentage) => _directive(DarkenColorDirective(percentage)); + T lighten(int percentage) => _directive(LightenColorDirective(percentage)); + T saturate(int percentage) => _directive(SaturateColorDirective(percentage)); + T desaturate(int percentage) => + _directive(DesaturateColorDirective(percentage)); + T tint(int percentage) => _directive(TintColorDirective(percentage)); + T shade(int percentage) => _directive(ShadeColorDirective(percentage)); + T brighten(int percentage) => _directive(BrightenColorDirective(percentage)); + + T of(ColorToken ref) => _buildColor(ref()); + T transparent() => _buildColor(Colors.transparent); T black() => _buildColor(const Color(0xFF000000)); T black87() => _buildColor(const Color(0xDD000000)); From 48be98be6d902e3d261a86ea36b8c6387533d15b Mon Sep 17 00:00:00 2001 From: Leo Farias Date: Fri, 2 Feb 2024 10:24:53 -0500 Subject: [PATCH 3/4] Clean up and further merge issues --- lib/src/attributes/gradient/gradient_dto.dart | 4 ++-- lib/src/attributes/shadow/shadow_dto.dart | 2 +- lib/src/attributes/text_style/text_style_dto.dart | 3 +-- lib/src/decorators/widget_decorators.dart | 1 + lib/src/specs/icon/icon_attribute.dart | 2 +- lib/src/specs/image/image_attribute.dart | 6 +++--- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/src/attributes/gradient/gradient_dto.dart b/lib/src/attributes/gradient/gradient_dto.dart index 062dbf8ff..685e76f8e 100644 --- a/lib/src/attributes/gradient/gradient_dto.dart +++ b/lib/src/attributes/gradient/gradient_dto.dart @@ -133,8 +133,8 @@ class LinearGradientDto extends GradientDto { end: other.end ?? end, tileMode: other.tileMode ?? tileMode, transform: other.transform ?? transform, - colors: colors?.merge(other.colors), - stops: stops?.merge(other.stops), + colors: colors?.merge(other.colors) ?? other.colors, + stops: stops?.merge(other.stops) ?? other.stops, ); } diff --git a/lib/src/attributes/shadow/shadow_dto.dart b/lib/src/attributes/shadow/shadow_dto.dart index 5f91e42a6..12bbbba10 100644 --- a/lib/src/attributes/shadow/shadow_dto.dart +++ b/lib/src/attributes/shadow/shadow_dto.dart @@ -67,7 +67,7 @@ class ShadowDto extends ShadowDtoImpl { return ShadowDto( blurRadius: other.blurRadius ?? blurRadius, - color: other.color ?? color, + color: color?.merge(other.color) ?? other.color, offset: other.offset ?? offset, ); } diff --git a/lib/src/attributes/text_style/text_style_dto.dart b/lib/src/attributes/text_style/text_style_dto.dart index 2cfe5c43b..cb156565e 100644 --- a/lib/src/attributes/text_style/text_style_dto.dart +++ b/lib/src/attributes/text_style/text_style_dto.dart @@ -6,7 +6,6 @@ import 'package:flutter/material.dart'; import '../../core/attribute.dart'; import '../../core/extensions/iterable_ext.dart'; -import '../../core/extensions/values_ext.dart'; import '../../factory/mix_provider_data.dart'; import '../../theme/tokens/text_style_token.dart'; import '../color/color_dto.dart'; @@ -108,7 +107,7 @@ class TextStyleData extends Dto with Mergeable { height: style.height, letterSpacing: style.letterSpacing, locale: style.locale, - shadows: style.shadows?.map((e) => e.toDto()).toList(), + shadows: style.shadows?.map(ShadowDto.from).toList(), textBaseline: style.textBaseline, wordSpacing: style.wordSpacing, ); diff --git a/lib/src/decorators/widget_decorators.dart b/lib/src/decorators/widget_decorators.dart index ed26b6d00..b8a1dba18 100644 --- a/lib/src/decorators/widget_decorators.dart +++ b/lib/src/decorators/widget_decorators.dart @@ -138,6 +138,7 @@ class ScaleDecorator extends WidgetDecorator { enum ClipType { path, oval, rect, rRect, triangle } +// TODO: Implement BorderRadiusGeometryDto class ClipDecorator extends WidgetDecorator with Mergeable { final ClipType clipType; diff --git a/lib/src/specs/icon/icon_attribute.dart b/lib/src/specs/icon/icon_attribute.dart index 7707bd70e..59da5bf03 100644 --- a/lib/src/specs/icon/icon_attribute.dart +++ b/lib/src/specs/icon/icon_attribute.dart @@ -20,7 +20,7 @@ class IconSpecAttribute extends SpecAttribute { return IconSpecAttribute( size: size ?? other.size, - color: color ?? other.color, + color: color?.merge(other.color) ?? other.color, ); } diff --git a/lib/src/specs/image/image_attribute.dart b/lib/src/specs/image/image_attribute.dart index 14bcaf1b9..747f841ce 100644 --- a/lib/src/specs/image/image_attribute.dart +++ b/lib/src/specs/image/image_attribute.dart @@ -48,15 +48,15 @@ class ImageSpecAttribute extends SpecAttribute { if (other == null) return this; return ImageSpecAttribute( + centerSlice: other.centerSlice ?? centerSlice, width: other.width ?? width, height: other.height ?? height, - color: other.color ?? color, + color: color?.merge(other.color) ?? other.color, repeat: other.repeat ?? repeat, fit: other.fit ?? fit, alignment: other.alignment ?? alignment, - centerSlice: other.centerSlice ?? centerSlice, - filterQuality: other.filterQuality ?? filterQuality, colorBlendMode: other.colorBlendMode ?? colorBlendMode, + filterQuality: other.filterQuality ?? filterQuality, ); } From d22bcfbbeee8cd8e737dd5b25125d5689ed41363 Mon Sep 17 00:00:00 2001 From: Leo Farias Date: Fri, 2 Feb 2024 10:34:28 -0500 Subject: [PATCH 4/4] Icon adjustments --- lib/src/specs/icon/icon_attribute.dart | 2 +- test/src/specs/icon/icon_attribute_test.dart | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/specs/icon/icon_attribute.dart b/lib/src/specs/icon/icon_attribute.dart index 59da5bf03..0a847b3d4 100644 --- a/lib/src/specs/icon/icon_attribute.dart +++ b/lib/src/specs/icon/icon_attribute.dart @@ -19,7 +19,7 @@ class IconSpecAttribute extends SpecAttribute { if (other == null) return this; return IconSpecAttribute( - size: size ?? other.size, + size: other.size ?? size, color: color?.merge(other.color) ?? other.color, ); } diff --git a/test/src/specs/icon/icon_attribute_test.dart b/test/src/specs/icon/icon_attribute_test.dart index 04804a56e..e4177d7e4 100644 --- a/test/src/specs/icon/icon_attribute_test.dart +++ b/test/src/specs/icon/icon_attribute_test.dart @@ -26,8 +26,8 @@ void main() { IconSpecAttribute(size: 32, color: ColorDto(Colors.green)); final mergedAttribute = attribute1.merge(attribute2); expect(mergedAttribute, isA()); - expect(mergedAttribute.size, equals(24)); - expect(mergedAttribute.color, equals(const ColorDto(Colors.red))); + expect(mergedAttribute.size, equals(32)); + expect(mergedAttribute.color, equals(const ColorDto(Colors.green))); }, );