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

[DecorationImage] adds scale property #54258

Merged
merged 4 commits into from Apr 18, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/flutter/lib/src/painting/decoration_image.dart
Expand Up @@ -47,10 +47,12 @@ class DecorationImage {
this.centerSlice,
this.repeat = ImageRepeat.noRepeat,
this.matchTextDirection = false,
this.scale = 1.0
}) : assert(image != null),
assert(alignment != null),
assert(repeat != null),
assert(matchTextDirection != null);
assert(matchTextDirection != null),
assert(scale != null);

/// The image to be painted into the decoration.
///
Expand Down Expand Up @@ -129,6 +131,12 @@ class DecorationImage {
/// in the top right.
final bool matchTextDirection;

/// Defines image pixels to be shown per logical pixels.
///
/// By default the the value of scale is 1.0. The scale for the image is
/// calculated by multiplying [scale] with `scale` of the given [ImageProvider].
final double scale;

/// Creates a [DecorationImagePainter] for this [DecorationImage].
///
/// The `onChanged` argument must not be null. It will be called whenever the
Expand All @@ -152,11 +160,12 @@ class DecorationImage {
&& other.alignment == alignment
&& other.centerSlice == centerSlice
&& other.repeat == repeat
&& other.matchTextDirection == matchTextDirection;
&& other.matchTextDirection == matchTextDirection
&& other.scale == scale;
}

@override
int get hashCode => hashValues(image, colorFilter, fit, alignment, centerSlice, repeat, matchTextDirection);
int get hashCode => hashValues(image, colorFilter, fit, alignment, centerSlice, repeat, matchTextDirection, scale);

@override
String toString() {
Expand All @@ -175,6 +184,7 @@ class DecorationImage {
'$repeat',
if (matchTextDirection)
'match text direction',
'scale: $scale'
];
return '${objectRuntimeType(this, 'DecorationImage')}(${properties.join(", ")})';
}
Expand Down Expand Up @@ -263,7 +273,7 @@ class DecorationImagePainter {
canvas: canvas,
rect: rect,
image: _image.image,
scale: _image.scale,
scale: _details.scale * _image.scale,
colorFilter: _details.colorFilter,
fit: _details.fit,
alignment: _details.alignment.resolve(configuration.textDirection),
Expand Down
32 changes: 31 additions & 1 deletion packages/flutter/test/painting/decoration_test.dart
Expand Up @@ -279,7 +279,7 @@ void main() {
' direction provided in the ImageConfiguration object to match.\n'
' The DecorationImage was:\n'
' DecorationImage(SynchronousTestImageProvider(), center, match\n'
' text direction)\n'
' text direction, scale: 1.0)\n'
' The ImageConfiguration was:\n'
' ImageConfiguration(size: Size(100.0, 100.0))\n'
);
Expand Down Expand Up @@ -583,4 +583,34 @@ void main() {
expect(call.positionalArguments[2].center, outputRect.center);
}
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also add a scale test similar to this paintImage scale test. The test in particular tests paintImage, but you can combine elements from this BoxDecoration test with the scale test for paintImage to achieve the outcome you need.

test('scale cannot be null in DecorationImage', () {
try {
DecorationImage(scale: null, image: SynchronousTestImageProvider());
} on AssertionError catch (error) {
expect(error.toString(), contains('scale != null'));
expect(error.toString(), contains('is not true'));
return;
}
fail('DecorationImage did not throw AssertionError when scale was null');
});

test('DecorationImage scale test', () {
final DecorationImage backgroundImage = DecorationImage(
image: SynchronousTestImageProvider(),
scale: 4,
alignment: Alignment.topLeft
);

final BoxDecoration boxDecoration = BoxDecoration(image: backgroundImage);
final BoxPainter boxPainter = boxDecoration.createBoxPainter(() { assert(false); });
final TestCanvas canvas = TestCanvas(<Invocation>[]);
boxPainter.paint(canvas, Offset.zero, const ImageConfiguration(size: Size(100.0, 100.0)));

final Invocation call = canvas.invocations.firstWhere((Invocation call) => call.memberName == #drawImageRect);
// The image should scale down to Size(25.0, 25.0) from Size(100.0, 100.0)
// considering DecorationImage scale to be 4.0 and Image scale to be 1.0.
expect(call.positionalArguments[2].size, const Size(25.0, 25.0));
expect(call.positionalArguments[2], const Rect.fromLTRB(0.0, 0.0, 25.0, 25.0));
});
}