diff --git a/packages/flutter/lib/src/painting/decoration_image.dart b/packages/flutter/lib/src/painting/decoration_image.dart index ebb2c6cc7a..65a56479e6 100644 --- a/packages/flutter/lib/src/painting/decoration_image.dart +++ b/packages/flutter/lib/src/painting/decoration_image.dart @@ -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. /// @@ -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 @@ -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() { @@ -175,6 +184,7 @@ class DecorationImage { '$repeat', if (matchTextDirection) 'match text direction', + 'scale: $scale' ]; return '${objectRuntimeType(this, 'DecorationImage')}(${properties.join(", ")})'; } @@ -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), diff --git a/packages/flutter/test/painting/decoration_test.dart b/packages/flutter/test/painting/decoration_test.dart index 2ebadaa59f..3d358cebe3 100644 --- a/packages/flutter/test/painting/decoration_test.dart +++ b/packages/flutter/test/painting/decoration_test.dart @@ -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' ); @@ -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([]); + 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)); + }); }