Flutter绘制拖尾效果

演示:

代码:

Dart 复制代码
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:kq_flutter_widgets/widgets/chart/ex/extension.dart';

class TrailingView extends StatelessWidget {
  const TrailingView({super.key});

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (v1, v2) {
      Path path = Path();
      path.moveTo(50, 50);
      path.cubicTo(50, 50, 100, 300, 300, 400);

      return CustomPaint(
        size: Size(v2.maxWidth, v2.maxHeight),
        painter: Trailing(path: path, r: 8),
      );
    });
  }
}

///拖尾效果
class Trailing extends CustomPainter {
  ///路径
  final Path path;

  ///起始值大小半径
  final double r;

  Trailing({
    required this.path,
    required this.r,
  });

  @override
  void paint(Canvas canvas, Size size) {
    PathMetric? pathMetric1 = path.computeMetric();
    if (pathMetric1 != null) {
      int length1 = pathMetric1.length.toInt();
      for (int i = 0; i < length1.toInt(); i++) {
        Tangent? tangent1 = pathMetric1.getTangentForOffset(i.toDouble());
        double mix = i / length1.toInt();
        if (tangent1 != null) {
          Offset cur = tangent1.position;
          canvas.drawCircle(
              cur,
              r * mix,
              Paint()
                ..color = Colors.redAccent
                ..maskFilter = const MaskFilter.blur(BlurStyle.normal, 8));
        }
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}

主要思路:

根据路径获取路径上的点,并从路径的起始点到终点绘制从大变小的圆点,且设置画笔为外发光模式,就可以达到一个可以根据路径显示的拖尾效果了。

相关推荐
每次的天空4 小时前
Flutter学习总结之Android渲染对比
android·学习·flutter
LinXunFeng6 小时前
Flutter - Xcode16 还原编译速度
前端·flutter·xcode
小墙程序员8 小时前
Flutter 教程(十四)动画
flutter
leluckys9 小时前
flutter 专题 六十八 Flutter 多图片上传
前端·javascript·flutter
小墙程序员11 小时前
Flutter 教程(十三)混合开发
flutter
Jalor13 小时前
Flutter 与 HarmonyOS NEXT | IAPKit(应用内支付服务)避坑指南
前端·flutter·harmonyos
小墙程序员1 天前
Flutter 教程(十二)异步编程
flutter
王喆1 天前
跨平台全屏效果实现方案:HarmonyOS、Android与iOS实践总结
flutter·harmonyos
Tee xm1 天前
清晰易懂的 Flutter 卸载和清理教程
linux·windows·flutter·macos
leluckys1 天前
flutter 专题 七十三Flutter打包未签名的ipa
flutter