Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to decoration image #192

Merged
merged 5 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/src/attributes/decoration/decoration_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '../border/shape_border_dto.dart';
import '../color/color_dto.dart';
import '../gradient/gradient_dto.dart';
import '../shadow/shadow_dto.dart';
import 'image/decoration_image_dto.dart';

/// A Data transfer object that represents a [Decoration] value.
///
Expand Down Expand Up @@ -99,6 +100,7 @@ class BoxDecorationDto extends DecorationDto<BoxDecoration> {
final BorderRadiusGeometryDto? borderRadius;
final BoxShape? shape;
final BlendMode? backgroundBlendMode;
final DecorationImageDto? image;

const BoxDecorationDto({
super.color,
Expand All @@ -108,6 +110,7 @@ class BoxDecorationDto extends DecorationDto<BoxDecoration> {
super.boxShadow,
this.shape,
this.backgroundBlendMode,
this.image,
});

/// Creates a [BoxDecorationDto] from a given [BoxDecoration].
Expand All @@ -119,6 +122,7 @@ class BoxDecorationDto extends DecorationDto<BoxDecoration> {
gradient: GradientDto.maybeFrom(decoration.gradient),
boxShadow: decoration.boxShadow?.map(BoxShadowDto.from).toList(),
shape: decoration.shape,
image: DecorationImageDto.maybeFrom(decoration.image),
backgroundBlendMode: decoration.backgroundBlendMode,
);
}
Expand All @@ -144,6 +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) ?? other.image,
);
}

Expand All @@ -167,6 +172,7 @@ class BoxDecorationDto extends DecorationDto<BoxDecoration> {
gradient: gradient?.resolve(mix),
backgroundBlendMode: backgroundBlendMode,
shape: shape ?? BoxShape.rectangle,
image: image?.resolve(mix),
);
}

Expand Down
11 changes: 10 additions & 1 deletion lib/src/attributes/decoration/decoration_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import '../scalars/scalar_util.dart';
import '../shadow/shadow_dto.dart';
import '../shadow/shadow_util.dart';
import 'decoration_dto.dart';
import 'image/decoration_image_dto.dart';
import 'image/decoration_image_util.dart';

class DecorationUtility<T extends StyleAttribute>
extends DtoUtility<T, DecorationDto, Decoration> {
Expand Down Expand Up @@ -48,6 +50,7 @@ class BoxDecorationUtility<T extends StyleAttribute>
List<BoxShadowDto>? boxShadow,
BoxShape? shape,
BlendMode? backgroundBlendMode,
DecorationImageDto? image,
}) {
return builder(
BoxDecorationDto(
Expand All @@ -58,6 +61,7 @@ class BoxDecorationUtility<T extends StyleAttribute>
boxShadow: boxShadow,
shape: shape,
backgroundBlendMode: backgroundBlendMode,
image: image,
),
);
}
Expand All @@ -72,7 +76,8 @@ class BoxDecorationUtility<T extends StyleAttribute>

BlendModeUtility<T> get backgroundBlendMode {
return BlendModeUtility(
(blendMode) => _only(backgroundBlendMode: blendMode));
(blendMode) => _only(backgroundBlendMode: blendMode),
);
}

BorderDirectionalUtility<T> get borderDirectional {
Expand Down Expand Up @@ -121,6 +126,10 @@ class BoxDecorationUtility<T extends StyleAttribute>
});
}

DecorationImageUtility<T> get image {
return DecorationImageUtility((image) => _only(image: image));
}

T call({
Color? color,
BoxBorder? border,
Expand Down
92 changes: 92 additions & 0 deletions lib/src/attributes/decoration/image/decoration_image_dto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:flutter/widgets.dart';

import '../../../core/attribute.dart';
import '../../../factory/mix_provider_data.dart';

@immutable
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({
this.image,
this.fit,
this.alignment,
this.centerSlice,
this.repeat,
this.filterQuality,
this.invertColors,
this.isAntiAlias,
});

static DecorationImageDto from(DecorationImage image) {
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,
);
}

static DecorationImageDto? maybeFrom(DecorationImage? image) {
return image != null ? from(image) : null;
}

@override
DecorationImageDto merge(covariant DecorationImageDto? other) {
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
DecorationImage resolve(MixData mix) {
const defaultDecoration = DecorationImage(image: AssetImage(''));

assert(
image != null,
'ImageProvider is required for DecorationImage',
);

return DecorationImage(
image: image!,
fit: fit,
alignment: alignment ?? defaultDecoration.alignment,
centerSlice: centerSlice,
repeat: repeat ?? defaultDecoration.repeat,
filterQuality: filterQuality ?? defaultDecoration.filterQuality,
invertColors: invertColors ?? defaultDecoration.invertColors,
isAntiAlias: isAntiAlias ?? defaultDecoration.isAntiAlias,
);
}

@override
List<Object?> get props => [
image,
fit,
alignment,
centerSlice,
repeat,
filterQuality,
invertColors,
isAntiAlias,
];
}
66 changes: 66 additions & 0 deletions lib/src/attributes/decoration/image/decoration_image_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:flutter/widgets.dart';

import '../../../core/attribute.dart';
import '../../scalars/scalar_util.dart';
import 'decoration_image_dto.dart';

class DecorationImageUtility<T extends StyleAttribute>
extends MixUtility<T, DecorationImageDto> {
const DecorationImageUtility(super.builder);

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,
),
);
}

BoxFitUtility<T> get fit {
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 call(ImageProvider image) => builder(DecorationImageDto(image: image));
}
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
@@ -0,0 +1,67 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mix/src/attributes/decoration/image/decoration_image_dto.dart';

void main() {
group('DecorationImageDto', () {
const imageProvider = AssetImage('assets/images/test.png');
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,
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,
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));
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mix/mix.dart';

import '../../helpers/testing_utils.dart';
import '../../../helpers/testing_utils.dart';

void main() {
group('Box', () {
Expand Down
Loading