问题:
Flutter 中的按钮如何自定义形状勒? 例如我想要一个三角形的按钮
解决方法:
使用CustomPainter
来实现:
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) { Paint 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; } }