Skip to content

Commit

Permalink
[DecorationImage] adds scale property (#54258)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushBherwani1998 committed Apr 18, 2020
1 parent 55cbf80 commit f35b673
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
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);
}
});

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

0 comments on commit f35b673

Please sign in to comment.