我已经创建了一个圆形按钮,在本例中是RawMaterialButton,我试图使用CustomPaint在它的中心创建一个三角形,但它说明ShapesPainter没有为类ClearButton'. I tried other buttons but couldn't get any of them to acceptShapePainter`定义.

RawMaterialButton(
          child: CustomPaint(
            painter: ShapesPainter(),
            child: Container(
              height: 40,
            ),
          ),
          onPressed: onPressed,
          constraints: BoxConstraints.tightFor(
            width: 90.0,
            height: 90.0,
          ),
          shape: RoundedRectangleBorder(),
          fillColor: Colors.transparent,
        )

ShapesPainter应该使用哪种类型的按钮,或者我如何创建中心带有三角形或其他形状的圆形按钮?

这是我试图创建的按钮,如您所见,它基本上是一个带三角形的圆形按钮.

enter image description here

推荐答案

您可以通过创建自己的自定义Paint实现来完成此操作.

Triangle Painter

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

USAGE

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,
            ),
          ),
        ),
      ),
    );
  }
}

Flutter相关问答推荐

关闭视频通话,相机仍在运行!(Flutter WebRTC)

获取屏幕大小的滚动视图与动态大小?

如何使用新的Riverpod语法将依赖传递给AsyncNotifier?

Dexing时出错.将Flutter 升级到3.19.0后

如何组合匹配器

在setState之后,Ffltter ListView.Builder未更新

Flutter 翼future 建造者不断开火和重建.它在每次重建时都会生成一个新的随机数--我如何防止这种情况?

GetConnect Post API调用在Flutter Web中不起作用说:415不支持的媒体类型

Riverpod Provider.family 和 HookConsumerWidget 不刷新 UI

打开键盘时屏幕组件会被压扁

在 Dart 中按土耳其语字母顺序对字符串进行排序

splash_screen 没有被推送到登录页面

Dart 撕掉 const 构造函数

我怎样才能消除我的 Column 子元素之间的差距?

为什么轮播加载图片很慢?

如何在手势检测器中获得涟漪效应

我一直在try 创建按钮以导航到第二页,但不知何故 RaisedButton 功能无法正常工作

为什么这个循环只发生一次?Flutter /dart

如何从 Flutter 中的列表中提取 JSON?

我想将我的模型类对象发送到下一个屏幕之前我这样做首先我在第二个屏幕中声明对象