Skip to content

Commit

Permalink
Merge pull request #191 from conceptadev/fix/issue-190
Browse files Browse the repository at this point in the history
Fix/issue 190
  • Loading branch information
leoafarias authored Feb 2, 2024
2 parents b209822 + d22bcfb commit 985e21e
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 54 deletions.
16 changes: 8 additions & 8 deletions lib/src/attributes/border/border_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ class BoxBorderDto extends Dto<BoxBorder> with Mergeable<BoxBorderDto> {
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,
);
}

Expand Down
61 changes: 40 additions & 21 deletions lib/src/attributes/border/border_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ class BorderSideUtility<T extends StyleAttribute>
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]
Expand All @@ -52,12 +54,16 @@ class BorderSideUtility<T extends StyleAttribute>
BorderStyleUtility<T> 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,
Expand All @@ -80,6 +86,7 @@ class BorderUtility<T extends StyleAttribute>
extends DtoUtility<T, BoxBorderDto, BoxBorder> {
/// Constructor for creating an instance of the class.
const BorderUtility(super.builder) : super(valueToDto: BoxBorderDto.from);

BoxBorderDto _symmetric({
BorderSideDto? vertical,
BorderSideDto? horizontal,
Expand All @@ -96,17 +103,6 @@ class BorderUtility<T extends StyleAttribute>
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<T> get _directional =>
BorderDirectionalUtility((border) => builder(border));

Expand All @@ -123,22 +119,22 @@ class BorderUtility<T extends StyleAttribute>

/// Method to set the border on the bottom side.
BorderSideUtility<T> get bottom {
return BorderSideUtility((side) => _only(bottom: side));
return BorderSideUtility((side) => only(bottom: side));
}

/// Method to set the border on the top side.
BorderSideUtility<T> get top {
return BorderSideUtility((side) => _only(top: side));
return BorderSideUtility((side) => only(top: side));
}

/// Method to set the border on the left side.
BorderSideUtility<T> get left {
return BorderSideUtility((side) => _only(left: side));
return BorderSideUtility((side) => only(left: side));
}

/// Method to set the border on the right side.
BorderSideUtility<T> get right {
return BorderSideUtility((side) => _only(right: side));
return BorderSideUtility((side) => only(right: side));
}

/// Method to set the borders on the vertical sides.
Expand All @@ -153,6 +149,29 @@ class BorderUtility<T extends StyleAttribute>
);
}

/// Returns a [ColorUtility] to manipulate [Color] of the [BorderSideDto]
ColorUtility<T> get color => all.color;

BorderStyleUtility<T> 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,
Expand Down
26 changes: 13 additions & 13 deletions lib/src/attributes/color/color_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ class ColorUtility<T extends StyleAttribute>
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<T> get red => MaterialColorUtility(builder, Colors.red);
MaterialColorUtility<T> get pink =>
MaterialColorUtility(builder, Colors.pink);
Expand Down Expand Up @@ -101,6 +88,19 @@ class ColorUtility<T extends StyleAttribute>
MaterialAccentColorUtility(builder, Colors.orangeAccent);
MaterialAccentColorUtility<T> 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));
Expand Down
4 changes: 2 additions & 2 deletions lib/src/attributes/gradient/gradient_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ class LinearGradientDto extends GradientDto<LinearGradient> {
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,
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/attributes/shadow/shadow_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ShadowDto extends ShadowDtoImpl<ShadowDto, Shadow> {

return ShadowDto(
blurRadius: other.blurRadius ?? blurRadius,
color: other.color ?? color,
color: color?.merge(other.color) ?? other.color,
offset: other.offset ?? offset,
);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/attributes/text_style/text_style_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -108,7 +107,7 @@ class TextStyleData extends Dto<TextStyle> with Mergeable<TextStyleData> {
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,
);
Expand Down
1 change: 1 addition & 0 deletions lib/src/decorators/widget_decorators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class ScaleDecorator extends WidgetDecorator<ScaleDecorator> {

enum ClipType { path, oval, rect, rRect, triangle }

// TODO: Implement BorderRadiusGeometryDto
class ClipDecorator<T> extends WidgetDecorator<ClipDecorator>
with Mergeable<ClipDecorator> {
final ClipType clipType;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/specs/icon/icon_attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class IconSpecAttribute extends SpecAttribute<IconSpecAttribute, IconSpec> {
if (other == null) return this;

return IconSpecAttribute(
size: size ?? other.size,
color: color ?? other.color,
size: other.size ?? size,
color: color?.merge(other.color) ?? other.color,
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/specs/image/image_attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class ImageSpecAttribute extends SpecAttribute<ImageSpecAttribute, ImageSpec> {
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,
);
}

Expand Down
31 changes: 31 additions & 0 deletions test/src/attributes/border/border_dto_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/src/specs/icon/icon_attribute_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void main() {
IconSpecAttribute(size: 32, color: ColorDto(Colors.green));
final mergedAttribute = attribute1.merge(attribute2);
expect(mergedAttribute, isA<IconSpecAttribute>());
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)));
},
);

Expand Down

0 comments on commit 985e21e

Please sign in to comment.