Flutter 颤振/省道-如何创建这样一个自定义形状的按钮?

Flutter 颤振/省道-如何创建这样一个自定义形状的按钮?,flutter,dart,Flutter,Dart,我想创建一个三个按钮,如图中所示,但我不确定如何做到这一点。有什么建议吗?您可以通过创建自己的自定义画师实现来实现 三角画师 class TrianglePainter extends CustomPainter { final Color strokeColor; final PaintingStyle paintingStyle; final double strokeWidth; TrianglePainter({this.strokeColor = Colors.bla

我想创建一个三个按钮,如图中所示,但我不确定如何做到这一点。有什么建议吗?

您可以通过创建自己的自定义画师实现来实现

三角画师

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;
  }
}
用法

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RawMaterialButton(
          onPressed: () {},
          child: CustomPaint(
            painter: TrianglePainter(
              strokeColor: Colors.blue,
              strokeWidth: 10,
              paintingStyle: PaintingStyle.fill,
            ),
            child: Container(
              height: 180,
              width: 200,
            ),
          ),
        ),
      ),
    );
  }
}