I'm working on a Flutter project and I need to create a custom shape that looks like the one shown below: enter image description here

我试着用fluttershapemaker,但我不能完全达到我需要的形状.

我听说过Flutter中的CustomPainter小部件,但我不熟悉它.有人能提供如何使用CustomPainter创建此特定形状的指导或建议其他方法来实现它吗?

任何帮助或建议将不胜感激.感谢您发送编修.

推荐答案

你可以用CustomPainter画出来:

class CustomShapePainter extends CustomPainter {
  const CustomShapePainter({
    required this.topRadius,
    required this.bottomRadius,
    required this.color,
  });

  final double topRadius;
  final double bottomRadius;
  final Color color;

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()..color = color;
    final path = Path()
      // Top side
      ..lineTo(size.width - topRadius, 0)
      // Top-right corner
      ..arcToPoint(
        Offset(size.width, topRadius),
        radius: Radius.circular(topRadius),
      )
      // Right side
      ..lineTo(size.width, size.height - bottomRadius)
      // Bottom-right corner
      ..arcToPoint(
        Offset(size.width - bottomRadius, size.height),
        radius: Radius.circular(bottomRadius),
      )
      // Bottom side
      ..lineTo(bottomRadius + topRadius, size.height)
      // Bottom-left corner
      ..arcToPoint(
        Offset(topRadius, size.height - bottomRadius),
        radius: Radius.circular(bottomRadius),
      )
      // Left side
      ..lineTo(topRadius, topRadius)
      // Top-right inverted corner
      ..arcToPoint(
        Offset.zero,
        radius: Radius.circular(topRadius),
        clockwise: false,
      );

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomShapePainter oldDelegate) {
    return oldDelegate.topRadius != topRadius ||
        oldDelegate.bottomRadius != bottomRadius ||
        oldDelegate.color != color;
  }
}

Working example on DartPad.

Flutter相关问答推荐

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

如何从外部控制有状态窗口小部件本身?

无法在主屏幕视图中设置当前日期容器'

我可以在iOS设备上使用Ffltter实现Google Pay按钮吗?

如何在表格日历标题下方添加文本?

Flutter /dart 列表.第一个子类列表中的Where()错误

如何将请求字段正确添加到DART多部分请求

摆动更改标记可轻敲文本按钮样式

在flutter中实现类似3D的可滚动堆叠分页列表

flatter_localizations错误在Null值上使用了Null判断运算符

如何在Dart中允许正则表达式中有一个空格,但允许其他字符为1或更多?

Flutter如何从深入嵌套的小部件中打开抽屉?

如果不刷新页面,我看不到从 Firestore 中提取的数据

Agora VideoCall 不显示远程视频网格

Android Electric eel Mac M1:运行 flutter doctor -v 时无法找到Bundle 的 java 版本

避免长单词中断

我的主屏幕上的 FutureBuilder 不断重新加载(但仅在其他屏幕上时)?

Flutter 上Denim blue colored颜色 的 colored颜色 矩阵代码是什么?

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

如何在Flutter 的 initState 中初始化 Firestore 查询