我想做有波浪形边框的圆形容器.为此,我使用了flutter_custom_clippers 2.0.0个包的StarClipper,并进行了一些更改.但是,我不能让它和我想要的一样.有谁能帮忙吗?

如果有其他办法,请告诉我.提前感谢您:)

以下是我的代码:

import 'dart:math' as math;

import 'package:flutter/widgets.dart';

/// Clip widget in star shape
class StarClipper extends CustomClipper<Path> {
  StarClipper(this.numberOfPoints);

  /// The number of points of the star
  final int numberOfPoints;

  late int counter = 0;

  @override
  Path getClip(Size size) {
    double width = size.width;

    double halfWidth = width / 2;

    double bigRadius = halfWidth;

    double radius = halfWidth / 1.11;

    double degreesPerStep = _degToRad(360 / numberOfPoints) as double;

    double halfDegreesPerStep = degreesPerStep / 2;

    var path = Path();

    double max = 2 * math.pi;

    path.moveTo(width, halfWidth);

    debugPrint('halfWidth: $halfWidth');
    debugPrint('bigRadius: $bigRadius');
    debugPrint('degreesPerStep: $degreesPerStep');
    debugPrint('max: $max');

    path.moveTo(
      halfWidth + radius * math.cos(0 + halfDegreesPerStep),
      halfWidth + radius * math.sin(0 + halfDegreesPerStep),
    );

    for (double step = 0; step < max + 1; step += degreesPerStep) {
      debugPrint('math.cos(step): ${math.cos(step)}');
      debugPrint('math.sin(step): ${math.sin(step)}');
      debugPrint(
          'math.cos(step + halfDegreesPerStep): ${math.cos(step + halfDegreesPerStep)}');
      debugPrint(
          'math.sin(step + halfDegreesPerStep): ${math.sin(step + halfDegreesPerStep)}');

      debugPrint('------step-------: $step');
      debugPrint('x 1: ${halfWidth + bigRadius * math.cos(step)}');
      debugPrint('y 1: ${halfWidth + bigRadius * math.sin(step)}');
      debugPrint(
          'x 2: ${halfWidth + radius * math.cos(step + halfDegreesPerStep)}');
      debugPrint(
          'y 2: ${halfWidth + radius * math.sin(step + halfDegreesPerStep)}');

      /*path.quadraticBezierTo(
        halfWidth + bigRadius * math.cos(step),
        halfWidth + bigRadius * math.sin(step),
        halfWidth + radius * math.cos(step + halfDegreesPerStep),
        halfWidth + radius * math.sin(step + halfDegreesPerStep),
      );*/

      if (counter % 2 == 0) {
        path.quadraticBezierTo(
          halfWidth + bigRadius * math.cos(step),
          halfWidth + bigRadius * math.sin(step),
          halfWidth + radius * math.cos(step + halfDegreesPerStep),
          halfWidth + radius * math.sin(step + halfDegreesPerStep),
        );
      } else {
        path.quadraticBezierTo(
          halfWidth + radius * math.cos(step),
          halfWidth + radius * math.sin(step),
          halfWidth + bigRadius * math.cos(step + halfDegreesPerStep),
          halfWidth + bigRadius * math.sin(step + halfDegreesPerStep),
        );
      }

      counter++;
    }
    path.close();
    return path;
  }

  num _degToRad(num deg) => deg * (math.pi / 180.0);

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    StarClipper oldie = oldClipper as StarClipper;
    return numberOfPoints != oldie.numberOfPoints;
  }
}

输出:

enter image description here

我想要如下所示

enter image description here

推荐答案

我使用与flutter_custom_clippers 2.0.0 package相同的StarClipper文件,在上面的代码中应用了一些微小的更改,从而实现了它.

double outerCurveRadius = width / 2.08;
double innerCurveRadius = width / 2.42;

for (double step = 0; step < max + 1; step += degreesPerStep) {
  if (counter % 2 == 0) {
    path.quadraticBezierTo(
      halfWidth + outerCurveRadius * math.cos(step),
      halfWidth + outerCurveRadius * math.sin(step),
      halfWidth + radius * math.cos(step + halfDegreesPerStep),
      halfWidth + radius * math.sin(step + halfDegreesPerStep),
    );
  } else {
    path.quadraticBezierTo(
      halfWidth + innerCurveRadius * math.cos(step),
      halfWidth + innerCurveRadius * math.sin(step),
      halfWidth + radius * math.cos(step + halfDegreesPerStep),
      halfWidth + radius * math.sin(step + halfDegreesPerStep),
    );
  }

  counter++;
}

输出:

enter image description here

Flutter相关问答推荐

ProviderContainer和GoRouter

Flutter 蜂窝-检索非基元数据类型列表

Firebase动态链接已弃用,不应在新项目中使用.Flutter 替代方案

Flutter API请求BadState响应

如何使排内 children 的身高与其他排内 children 的身高相适应?

如何在Flutter 安全存储器中存储对象值?

Flutter GestureDetector单击问题-堆叠外面的按钮不可点击

Flutter 日期时间格式

Flutter卡内容未在水平列表视图中居中

flutter Listview构建器溢出

Flutter - Riverpod 2.0 - 如何访问 onDispose 回调?

Android 模拟器在 Apple M2 芯片 EXC_BAD_ACCESS / KERN_INVALID_ADDRESS 上随机崩溃

当我使用 Confetti Widget 时,Flutter Android 和 IOS 应用程序崩溃

应用程序包含嵌入式私钥或密钥库文件

Flutter使Perspective Page View无限循环

在不丢失游戏状态的情况下从 Flame 游戏导航到 Flutter 小部件

如何限制用户每 24 小时执行的操作数?

如何根据字符串值动态构建和显示小部件?

Flutter - 使用 pin 进行身份验证

Flutter 中的 Firebase 后台消息处理程序产生重复的隔离并运行应用程序两次