diff --git a/packages/flutter/lib/src/painting/box_fit.dart b/packages/flutter/lib/src/painting/box_fit.dart index 25b2e84a90..b80ce82799 100644 --- a/packages/flutter/lib/src/painting/box_fit.dart +++ b/packages/flutter/lib/src/painting/box_fit.dart @@ -166,12 +166,26 @@ FittedSizes applyBoxFit(BoxFit fit, Size inputSize, Size outputSize) { destinationSize = outputSize; break; case BoxFit.fitWidth: - sourceSize = Size(inputSize.width, inputSize.width * outputSize.height / outputSize.width); - destinationSize = Size(outputSize.width, sourceSize.height * outputSize.width / sourceSize.width); + if (outputSize.width / outputSize.height > inputSize.width / inputSize.height) { + // Like "cover" + sourceSize = Size(inputSize.width, inputSize.width * outputSize.height / outputSize.width); + destinationSize = outputSize; + } else { + // Like "contain" + sourceSize = inputSize; + destinationSize = Size(outputSize.width, sourceSize.height * outputSize.width / sourceSize.width); + } break; case BoxFit.fitHeight: - sourceSize = Size(inputSize.height * outputSize.width / outputSize.height, inputSize.height); - destinationSize = Size(sourceSize.width * outputSize.height / sourceSize.height, outputSize.height); + if (outputSize.width / outputSize.height > inputSize.width / inputSize.height) { + // Like "contain" + sourceSize = inputSize; + destinationSize = Size(sourceSize.width * outputSize.height / sourceSize.height, outputSize.height); + } else { + // Like "cover" + sourceSize = Size(inputSize.height * outputSize.width / outputSize.height, inputSize.height); + destinationSize = outputSize; + } break; case BoxFit.none: sourceSize = Size(math.min(inputSize.width, outputSize.width), math.min(inputSize.height, outputSize.height)); diff --git a/packages/flutter/test/painting/box_fit_test.dart b/packages/flutter/test/painting/box_fit_test.dart index 13c8bfb31e..acad45addb 100644 --- a/packages/flutter/test/painting/box_fit_test.dart +++ b/packages/flutter/test/painting/box_fit_test.dart @@ -21,10 +21,18 @@ void main() { expect(result.source, equals(const Size(2000.0, 200.0))); expect(result.destination, equals(const Size(1000.0, 100.0))); + result = applyBoxFit(BoxFit.fitWidth, const Size(2000.0, 400.0), const Size(1000.0, 300.0)); + expect(result.source, equals(const Size(2000.0, 400.0))); + expect(result.destination, equals(const Size(1000.0, 200.0))); + result = applyBoxFit(BoxFit.fitHeight, const Size(400.0, 2000.0), const Size(100.0, 1000.0)); expect(result.source, equals(const Size(200.0, 2000.0))); expect(result.destination, equals(const Size(100.0, 1000.0))); + result = applyBoxFit(BoxFit.fitHeight, const Size(400.0, 2000.0), const Size(300.0, 1000.0)); + expect(result.source, equals(const Size(400.0, 2000.0))); + expect(result.destination, equals(const Size(200.0, 1000.0))); + _testZeroAndNegativeSizes(BoxFit.fill); _testZeroAndNegativeSizes(BoxFit.contain); _testZeroAndNegativeSizes(BoxFit.cover); diff --git a/packages/flutter/test/painting/decoration_test.dart b/packages/flutter/test/painting/decoration_test.dart index c7f7ea9871..f92447c19d 100644 --- a/packages/flutter/test/painting/decoration_test.dart +++ b/packages/flutter/test/painting/decoration_test.dart @@ -669,6 +669,86 @@ void main() { } }); + test('paintImage with repeatX and fitHeight', () async { + final TestCanvas canvas = TestCanvas(); + + // Paint a square image into an output rect that is twice as wide as it is + // tall. Two copies of the image should be painted, one next to the other. + const Rect outputRect = Rect.fromLTWH(30.0, 30.0, 400.0, 200.0); + final ui.Image image = await createTestImage(width: 100, height: 100); + + paintImage( + canvas: canvas, + rect: outputRect, + image: image, + alignment: Alignment.topLeft, + fit: BoxFit.fitHeight, + repeat: ImageRepeat.repeatX, + ); + + const Size imageSize = Size(100.0, 100.0); + + final List calls = canvas.invocations.where((Invocation call) => call.memberName == #drawImageRect).toList(); + final Set tileRects = {}; + + expect(calls, hasLength(2)); + for (final Invocation call in calls) { + expect(call.isMethod, isTrue); + expect(call.positionalArguments, hasLength(4)); + + expect(call.positionalArguments[0], isA()); + + // sourceRect should contain all pixels of the source image + expect(call.positionalArguments[1], Offset.zero & imageSize); + + tileRects.add(call.positionalArguments[2] as Rect); + + expect(call.positionalArguments[3], isA()); + } + + expect(tileRects, {const Rect.fromLTWH(30.0, 30.0, 200.0, 200.0), const Rect.fromLTWH(230.0, 30.0, 200.0, 200.0)}); + }); + + test('paintImage with repeatY and fitWidth', () async { + final TestCanvas canvas = TestCanvas(); + + // Paint a square image into an output rect that is twice as tall as it is + // wide. Two copies of the image should be painted, one above the other. + const Rect outputRect = Rect.fromLTWH(30.0, 30.0, 200.0, 400.0); + final ui.Image image = await createTestImage(width: 100, height: 100); + + paintImage( + canvas: canvas, + rect: outputRect, + image: image, + alignment: Alignment.topLeft, + fit: BoxFit.fitWidth, + repeat: ImageRepeat.repeatY, + ); + + const Size imageSize = Size(100.0, 100.0); + + final List calls = canvas.invocations.where((Invocation call) => call.memberName == #drawImageRect).toList(); + final Set tileRects = {}; + + expect(calls, hasLength(2)); + for (final Invocation call in calls) { + expect(call.isMethod, isTrue); + expect(call.positionalArguments, hasLength(4)); + + expect(call.positionalArguments[0], isA()); + + // sourceRect should contain all pixels of the source image + expect(call.positionalArguments[1], Offset.zero & imageSize); + + tileRects.add(call.positionalArguments[2] as Rect); + + expect(call.positionalArguments[3], isA()); + } + + expect(tileRects, {const Rect.fromLTWH(30.0, 30.0, 200.0, 200.0), const Rect.fromLTWH(30.0, 230.0, 200.0, 200.0)}); + }); + test('DecorationImage scale test', () async { final ui.Image image = await createTestImage(width: 100, height: 100); final DecorationImage backgroundImage = DecorationImage(