diff --git a/packages/flutter/lib/src/painting/decoration_image.dart b/packages/flutter/lib/src/painting/decoration_image.dart index 60e151ee28..de0da406de 100644 --- a/packages/flutter/lib/src/painting/decoration_image.dart +++ b/packages/flutter/lib/src/painting/decoration_image.dart @@ -448,12 +448,9 @@ void paintImage({ Size inputSize = Size(image.width.toDouble(), image.height.toDouble()); Offset? sliceBorder; if (centerSlice != null) { - sliceBorder = Offset( - centerSlice.left + inputSize.width - centerSlice.right, - centerSlice.top + inputSize.height - centerSlice.bottom, - ); + sliceBorder = inputSize / scale - centerSlice.size as Offset; outputSize = outputSize - sliceBorder as Size; - inputSize = inputSize - sliceBorder as Size; + inputSize = inputSize - sliceBorder * scale as Size; } fit ??= centerSlice == null ? BoxFit.scaleDown : BoxFit.fill; assert(centerSlice == null || (fit != BoxFit.none && fit != BoxFit.cover)); @@ -559,7 +556,7 @@ void paintImage({ } } - final bool needSave = repeat != ImageRepeat.noRepeat || flipHorizontally; + final bool needSave = centerSlice != null || repeat != ImageRepeat.noRepeat || flipHorizontally; if (needSave) canvas.save(); if (repeat != ImageRepeat.noRepeat) @@ -581,11 +578,12 @@ void paintImage({ canvas.drawImageRect(image, sourceRect, tileRect, paint); } } else { + canvas.scale(1 / scale); if (repeat == ImageRepeat.noRepeat) { - canvas.drawImageNine(image, centerSlice, destinationRect, paint); + canvas.drawImageNine(image, _scaleRect(centerSlice, scale), _scaleRect(destinationRect, scale), paint); } else { for (final Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) - canvas.drawImageNine(image, centerSlice, tileRect, paint); + canvas.drawImageNine(image, _scaleRect(centerSlice, scale), _scaleRect(tileRect, scale), paint); } } if (needSave) @@ -619,3 +617,5 @@ Iterable _generateImageTileRects(Rect outputRect, Rect fundamentalRect, Im yield fundamentalRect.shift(Offset(i * strideX, j * strideY)); } } + +Rect _scaleRect(Rect rect, double scale) => Rect.fromLTRB(rect.left * scale, rect.top * scale, rect.right * scale, rect.bottom * scale); diff --git a/packages/flutter/test/painting/paint_image_test.dart b/packages/flutter/test/painting/paint_image_test.dart index d2c37fedac..1f52caa8f4 100644 --- a/packages/flutter/test/painting/paint_image_test.dart +++ b/packages/flutter/test/painting/paint_image_test.dart @@ -108,6 +108,26 @@ void main() { FlutterError.onError = oldFlutterError; }); + test('centerSlice with scale ≠ 1', () async { + final TestCanvas canvas = TestCanvas(); + paintImage( + canvas: canvas, + rect: const Rect.fromLTRB(10, 20, 430, 420), + image: image300x300, + scale: 2.0, + centerSlice: const Rect.fromLTRB(50, 40, 250, 260), + ); + + final Invocation command = canvas.invocations.firstWhere((Invocation invocation) { + return invocation.memberName == #drawImageNine; + }); + + expect(command, isNotNull); + expect(command.positionalArguments[0], equals(image300x300)); + expect(command.positionalArguments[1], equals(const Rect.fromLTRB(100.0, 80.0, 500.0, 520.0))); + expect(command.positionalArguments[2], equals(const Rect.fromLTRB(20.0, 40.0, 860.0, 840.0))); + }); + testWidgets('Reports Image painting', (WidgetTester tester) async { late ImageSizeInfo imageSizeInfo; int count = 0;