代码
class TrianglePainter extends CustomPainter {
final Color strokeColor;
final PaintingStyle paintingStyle;
final double strokeWidth;
TrianglePainter(
{this.strokeColor = Colors.black,
this.strokeWidth = 3,
this.paintingStyle = PaintingStyle.stroke});
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color = strokeColor
..strokeWidth = strokeWidth
..style = paintingStyle;
canvas.drawPath(getTrianglePath(size.width, size.height), paint);
}
Path getTrianglePath(double x, double y) {
return Path()
..moveTo(0, y)
..lineTo(x / 2, 0)
..lineTo(x, y)
..lineTo(0, y);
}
@override
bool shouldRepaint(TrianglePainter oldDelegate) {
return oldDelegate.strokeColor != strokeColor ||
oldDelegate.paintingStyle != paintingStyle ||
oldDelegate.strokeWidth != strokeWidth;
}
}
使用
CustomPaint(
painter: TrianglePainter(
strokeColor: Colors.blue,
paintingStyle:
PaintingStyle.fill,
),
child: Container(
height: 10,
width: 12,
),
),
1.来源:https://stackoverflow.com/questions/56930636/flutter-button-with-custom-shape-triangle
2.使用边框实现空心三角形>>>