From e46f480733093da3478f15ce04317feabe7096c4 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sat, 5 Dec 2015 10:40:00 -0800 Subject: [PATCH] Repeated background images should be clipped to the container Fixes #766 --- packages/flutter/lib/src/painting/box_painter.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/flutter/lib/src/painting/box_painter.dart b/packages/flutter/lib/src/painting/box_painter.dart index 7a5355d060..75522713f8 100644 --- a/packages/flutter/lib/src/painting/box_painter.dart +++ b/packages/flutter/lib/src/painting/box_painter.dart @@ -669,6 +669,11 @@ void paintImage({ // as we apply a nine-patch stretch. assert(sourceSize == inputSize); } + if (repeat != ImageRepeat.noRepeat && destinationSize == outputSize) { + // There's no need to repeat the image because we're exactly filling the + // output rect with the image. + repeat = ImageRepeat.noRepeat; + } Paint paint = new Paint()..isAntiAlias = false; if (colorFilter != null) paint.colorFilter = colorFilter; @@ -676,6 +681,10 @@ void paintImage({ double dy = (outputSize.height - destinationSize.height) * (alignY ?? 0.5); Point destinationPosition = rect.topLeft + new Offset(dx, dy); Rect destinationRect = destinationPosition & destinationSize; + if (repeat != ImageRepeat.noRepeat) { + canvas.save(); + canvas.clipRect(rect); + } if (centerSlice == null) { Rect sourceRect = Point.origin & sourceSize; for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) @@ -684,6 +693,8 @@ void paintImage({ for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) canvas.drawImageNine(image, centerSlice, tileRect, paint); } + if (repeat != ImageRepeat.noRepeat) + canvas.restore(); } /// A background image for a box.