Skip to content

Commit

Permalink
add more attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tilucasoli committed Feb 2, 2024
1 parent 6d19a9f commit 7ec1c31
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/src/attributes/decoration/decoration_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class BoxDecorationDto extends DecorationDto<BoxDecoration> {
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,
);
}

Expand Down
38 changes: 37 additions & 1 deletion lib/src/attributes/decoration/image/decoration_image_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,35 @@ class DecorationImageDto extends Dto<DecorationImage>
with Mergeable<DecorationImageDto> {
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) {
if (image == null) return null;
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,
);
}

Expand All @@ -25,20 +43,38 @@ class DecorationImageDto extends Dto<DecorationImage>
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,
);
}

@override
List<Object?> get props => [
image,
fit,
alignment,
centerSlice,
repeat,
filterQuality,
invertColors,
isAntiAlias,
];

@override
DecorationImage resolve(MixData mix) {
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,
);
}
}
37 changes: 37 additions & 0 deletions lib/src/attributes/decoration/image/decoration_image_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,51 @@ class DecorationImageUtility<T extends StyleAttribute>
return BoxFitUtility((fit) => _only(fit: fit));
}

AlignmentUtility<T> get alignment {
return AlignmentUtility((alignment) => _only(alignment: alignment));
}

RectUtility<T> get centerSlice {
return RectUtility((centerSlice) => _only(centerSlice: centerSlice));
}

ImageRepeatUtility<T> get repeat {
return ImageRepeatUtility((repeat) => _only(repeat: repeat));
}

FilterQualityUtility<T> get filterQuality {
return FilterQualityUtility(
(filterQuality) => _only(filterQuality: filterQuality));
}

BoolUtility<T> get invertColors {
return BoolUtility((invertColors) => _only(invertColors: invertColors));
}

BoolUtility<T> 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,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions test/src/attributes/decoration/decoration_dto_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
}

0 comments on commit 7ec1c31

Please sign in to comment.