[英]Flutter adding border to CustomPaint
我正在使用CustomPainter
創建一個三角形。 我想創建一個帶邊框的三角形。
我的 PaintTriangle class:
class PaintTriangle extends CustomPainter {
final Color backgroundColor;
PaintTriangle({
required this.backgroundColor,
});
@override
void paint(Canvas canvas, Size size) {
final y = size.height;
final x = size.width;
final paint = Paint()..color = backgroundColor;
final path = Path()
..moveTo(0, y)
..lineTo((x / 2), (y / y))
..lineTo(x, y);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
您可以在 canvas 上再畫一幅畫。
class PaintTriangle extends CustomPainter {
final Color backgroundColor;
final Color borderColor;
final double borderThickness;
PaintTriangle(
{required this.backgroundColor,
required this.borderColor,
this.borderThickness = 4.0});
@override
void paint(Canvas canvas, Size size) {
final y = size.height;
final x = size.width;
final paint = Paint()..color = backgroundColor;
final path = Path()
..moveTo(0, y)
..lineTo((x / 2), (y / y))
..lineTo(x, y)
..lineTo(0, y); //little modification to draw bottom
final borderPaint = Paint()
..color = borderColor
..style = PaintingStyle.stroke
..strokeWidth = borderThickness;
canvas.drawPath(path, paint);
canvas.drawPath(path, borderPaint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
並使用
CustomPaint(
size: Size(200, 200),
painter: PaintTriangle(
backgroundColor: Colors.blue,
borderColor: Colors.red,
),
),
結果
您將需要為邊框制作另一個自定義畫家,其路徑與三角形相同。
此鏈接將回答您的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.