flutter - 带圆角的方形进度条

标签 flutter dart

我正在尝试在 dart/flutter 中构建一种圆形方形进度条。 也许有人知道如何使其成为可能?我尝试了所有,绘画,边框等等,但没有成功。

example image - circular squared progress bar

最佳答案

您可以尝试square_percent_indicaterhere .

如果您想要更加自定义的小部件,请尝试 CustomPainter:

class _MyBorderPainter extends CustomPainter {
  _MyBorderPainter(
      {required this.progress, required this.borderColor, this.strokeWdth});
  double progress; // desirable value for corners side
  double? strokeWdth;
  Color borderColor;

  @override
  void paint(Canvas canvas, Size size) {
    double x = min(size.height, size.width);
    double x2 = x / 2;
    double x4 = x / 4;

    Paint paintt = new Paint()
      ..color = progress == 0 ? Colors.transparent : borderColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeWdth ?? 4.0;
    // ..strokeCap = StrokeCap.round;

    Path path = Path()
      ..moveTo(x2, x)
      ..lineTo(x4, x)
      ..quadraticBezierTo(0, x, 0, x - x4)
      ..lineTo(0, x4)
      ..quadraticBezierTo(0, 0, x4, 0)
      ..lineTo(x - x4, 0)
      ..quadraticBezierTo(x, 0, x, x4)
      ..lineTo(x, x - x4)
      ..quadraticBezierTo(x, x, x - x4, x)
      ..lineTo(x2, x);

    PathMetric pathMetric = path.computeMetrics().first;

    Path extractPath = pathMetric.extractPath(
        pathMetric.length * (0.5 * (1 - progress)),
        pathMetric.length * (0.5 + 0.5 * progress));
    canvas.drawPath(extractPath, paintt);
  }

  @override
  bool shouldRepaint(covariant _MyBorderPainter oldDelegate) {
    return oldDelegate.progress != progress;
  }
}

class SquareProgressBar extends StatelessWidget {
  const SquareProgressBar(
      {required this.strokeWdth,
      required this.borderColor,
      this.borderBgColor,
      required this.progress,
      this.child,
      Key? key})
      : super(key: key);
  final double strokeWdth;
  final Color borderColor;
  final Color? borderBgColor;
  final double progress;
  final Widget? child;

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
        painter: _MyBorderPainter(
            borderColor: borderBgColor ?? Colors.grey,
            strokeWdth: strokeWdth,
            progress: 1.0),
        child: CustomPaint(
          painter: _MyBorderPainter(
            progress: this.progress,
            borderColor: borderColor,
            strokeWdth: strokeWdth,
          ),
          child: this.child ?? null,
        ));
  }
}

结果是:

关于flutter - 带圆角的方形进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65011661/

相关文章:

firebase - 如何防止 Firebase Firestore 在 Flutter 应用程序中进行不必要的读取操作?

ios - 如何使用FCM向iOS发送通知?

firebase - Flutter 应用程序因 dart.html 而停止工作

dart - 在新的Dart项目中未看到的Dart模板

java - Flutter/Java Plot Mp3 频率强度

Dart:为什么常量变量不能是实例变量?

flutter - 我们应该用 Flutter 不同的导航选项选择哪一个?

android-studio - 如何在 Android Studio 中为 Flutter 项目启用自动热重载?

android - 一次性读取Firebase云( Dart/flutter )

android - Webview 中的全屏视频在 Flutter 中不起作用