Skip to content

Instantly share code, notes, and snippets.

@BarryDaBee
Created February 20, 2025 09:05
Show Gist options
  • Save BarryDaBee/82cad72b83fb0722d1d0f1351ce9a00f to your computer and use it in GitHub Desktop.
Save BarryDaBee/82cad72b83fb0722d1d0f1351ce9a00f to your computer and use it in GitHub Desktop.
CustomPainter for diagonal stripes. PS: Check the first comment
import 'package:flutter/material.dart';
class StripesPainter extends CustomPainter {
StripesPainter({required this.stripeColor});
final Color stripeColor;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = stripeColor
..strokeWidth = 10;
var stepX = .0;
var stepY = size.height;
const step = 10;
while (stepX < size.width * 2) {
canvas.drawLine(
Offset(-4, stepY -= step),
Offset(stepX += step, size.height + 4),
paint,
);
stepY -= step * 2;
stepX += step * 2;
}
}
@override
bool shouldRepaint(StripesPainter oldDelegate) => false;
}
@BarryDaBee
Copy link
Author

BarryDaBee commented Feb 20, 2025

/// Use ClipRect to prevent stripes from drawing out of parents widget bounds
ClipRect(
    child: CustomPaint(
        painter: StripesPainter(
            stripeColor: Colors.red,  // Choose stripe color
        )
    )
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment