Skip to content

Commit e46f480

Browse files
committed
Repeated background images should be clipped to the container
Fixes flutter#766
1 parent 3419b3e commit e46f480

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

packages/flutter/lib/src/painting/box_painter.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,13 +669,22 @@ void paintImage({
669669
// as we apply a nine-patch stretch.
670670
assert(sourceSize == inputSize);
671671
}
672+
if (repeat != ImageRepeat.noRepeat && destinationSize == outputSize) {
673+
// There's no need to repeat the image because we're exactly filling the
674+
// output rect with the image.
675+
repeat = ImageRepeat.noRepeat;
676+
}
672677
Paint paint = new Paint()..isAntiAlias = false;
673678
if (colorFilter != null)
674679
paint.colorFilter = colorFilter;
675680
double dx = (outputSize.width - destinationSize.width) * (alignX ?? 0.5);
676681
double dy = (outputSize.height - destinationSize.height) * (alignY ?? 0.5);
677682
Point destinationPosition = rect.topLeft + new Offset(dx, dy);
678683
Rect destinationRect = destinationPosition & destinationSize;
684+
if (repeat != ImageRepeat.noRepeat) {
685+
canvas.save();
686+
canvas.clipRect(rect);
687+
}
679688
if (centerSlice == null) {
680689
Rect sourceRect = Point.origin & sourceSize;
681690
for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
@@ -684,6 +693,8 @@ void paintImage({
684693
for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
685694
canvas.drawImageNine(image, centerSlice, tileRect, paint);
686695
}
696+
if (repeat != ImageRepeat.noRepeat)
697+
canvas.restore();
687698
}
688699

689700
/// A background image for a box.

0 commit comments

Comments
 (0)