Flutter 自定义圆形进度条

本文详细介绍了一种自定义圆形进度条的实现方法,通过Flutter的CustomPainter类创建了一个CircleProgressBar组件,该组件能够根据传入的进度参数动态地更新显示效果。文章深入解析了画布(Canvas)、画笔(Paint)的使用,以及如何通过弧形(Arc)来表现进度状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图


//进度条
class CircleProgressBar extends CustomPainter {
  Paint _paintBackground;
  Paint _paintFore;
  final double pi = 3.1415926;
  var progress; //0-360

  CircleProgressBar(this.progress) {
    _paintBackground = Paint()
      ..color = Colors.transparent
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.0
      ..isAntiAlias = true;
    _paintFore = Paint()
      ..color = Color(BaseTheme.BASE_COLOR)
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.0
      ..isAntiAlias = true;
  }

  updateProgress(int pg) {
    this.progress = pg;
    print(progress);
  }

  @override
  void paint(Canvas canvas, Size size) {
    //绘制流程
    canvas.drawCircle(Offset(size.width / 2, size.height / 2), size.width / 2,
        _paintBackground);
    Rect rect = Rect.fromCircle(
      center: Offset(size.width / 2, size.height / 2),
      radius: size.width / 2,
    );
    canvas.drawArc(
        rect,
        -pi / 2, //圆开始的位置(正上方)
        360 * progress / 100 * 3.14 / 180,
        false,
        _paintFore);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    //刷新布局的时候告诉Flutter是否需要重绘
    return true;
  }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值