请考虑:

Expected rounded triangle

我怎样才能做出像图中那样的圆角三角形(带边框)?

我写了以下内容,但它看起来很奇怪,边界被剪裁了.我想要留着.

Clipped rounded triangle

ClipPath(
  clipper: MyLeftArrowClipper(),
  child: Container(
    // width: 100,
    // height: 50,
    decoration: BoxDecoration(
        color: Colors.amber, border: Border.all()),
    child: Text('time'),
  ),
),
class MyLeftArrowClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    printDebug('size= $size');
    var firstOffset = Offset(0, size.height * 0.5);
    var secondPoint = Offset(size.width * 0.15, 0);
    var right1 = Offset(size.width, 0);
    var right2 = Offset(size.width, size.height);
    var left1 = Offset(size.width * 0.15, size.height);
    var path = Path()
      ..moveTo(firstOffset.dx, firstOffset.dy)
      ..lineTo(secondPoint.dx, secondPoint.dy)
      ..lineTo(right1.dx, right1.dy)
      ..lineTo(right2.dx, right2.dy)
      ..lineTo(left1.dx, left1.dy)
      ..lineTo(firstOffset.dx, firstOffset.dy)
      ..close();

    return path;
  }

  @override
  bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
    return true;
  }
}

推荐答案

此CustomButtonPainter类与CustomPaint小部件一起使用,以绘制与图像中的按钮类似的按钮.CustomPaint小部件允许您提供一个大小,用于确定在其上绘制按钮的画布的大小.根据需要调整 colored颜色 、文本和大小,以匹配按钮的特定外观.

class CustomButtonPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.white // Change this to match the button's fill color
      ..style = PaintingStyle.fill;

    final strokePaint = Paint()
      ..color =
          Colors.grey.shade400 // Change this to match the button's border color
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1;

    // Calculate the notch size based on the overall size of the button
    final notchSize = size.height / 4;

    // Create the path for the button
    Path path = Path()
      ..moveTo(notchSize, 0)
      ..lineTo(size.width, 0)
      ..lineTo(size.width, size.height)
      ..lineTo(notchSize, size.height)
      ..lineTo(0, size.height / 2)
      ..close();

    // Draw the button
    canvas.drawPath(path, paint);
    canvas.drawPath(path, strokePaint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}

// Usage:
SizedBox(
  width: 200, // Set the width of the button
  height: 50, // Set the height of the button
  child: CustomPaint(
    painter: CustomButtonPainter(),
    child: Container(), // Can be replaced with actual child widget
  ),
)

Ios相关问答推荐

如何在iPhone 15上消除.NET Maui中状态栏和页面之间的差距

在iOS中禁用URLSession自动重试机制

带有RadStudio 11.3/12的Mac Mini M2(Sonoma 14.2.1)上的PAServer-测试连接按钮有效,但部署不起作用

为什么iOS委派在KMM中不能正常工作?

使用 Objective-C 创建 iOS 框架

SwiftUI中的Tap手势无法识别视图上的偏移变化

创建 Flutter 项目时,如何从我的系统中删除特定的开发者身份?

使用按位|带有布尔操作数

无法从 Xcode 获取模拟器列表.请打开 Xcode 并try 直接从那里运行项目以解决剩余问题

如何在 SwiftUI 中将选项卡放在滚动视图中?

如何在 SwiftUI 中只显示一次随机数组的项目?

带有属性包装器的不可用属性

如何阻止图像在 UIImageView 中拉伸?

iPhone 6 和 6 Plus 媒体查询

如何忽略touch 事件并将它们传递给另一个子视图的 UIControl 对象?

iOS 11 navigationItem.titleView 宽度未设置

如何更改uitableview删除按钮文本

在 iOS 7 中更改返回按钮会禁用滑动导航返回

类 AMSupportURLConnectionDelegate 在两者中都实现

如何创建 Select 器数组