Flutter 向 CustomPaint 添加边框
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,
),
),
结果
我正在使用 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,
),
),
结果