Flutter 绘制虚线

Flutter 没有自带的虚线绘制工具,需要自己绘制,下面提供一个方法来绘制吧,可以作为Paint的扩展,也可以直接用这个方法绘制。上代码。

Dart 复制代码
void dashPaint(
      Canvas canvas, startdx, startdy, enddx,enddy, dashWidth, gapWidth, Paint paint) {
    // 使用循环来模拟虚线效果
// dashWidth 虚线的宽度,gapWidth的宽度, start起点坐标,end终点坐标
    double currentX = startdx;
    double currentY = startdy;
    double d= sqrt((enddx- startdx)*(enddx- startdx)+(enddy- startdy)*(enddy- startdy)); //计算线段长度
    // sin
    double sin = (enddy - startdy) /d;
    double cos = (enddx - startdx) /d;
    while ((sin>0&&currentY < enddy) ||(sin<0&&currentY > enddy)){
      canvas.drawLine(
        Offset(currentX, currentY),
        Offset(currentX+ dashWidth* cos, currentY + dashWidth * sin ),
        paint,
      );
      currentX += (dashWidth + gapWidth)* cos;
      currentY += (dashWidth + gapWidth)* sin;
    }
  }
相关推荐
tianbin9112 分钟前
批处理与微调辅助场景:Claude 中转并发、链路缓存与压测对比
java·开发语言·缓存
小冯不疯5 分钟前
688 / 拼多多 / 淘特三类供货链路:从采购单到应付凭证的 4 段对账
javascript
frjc11 分钟前
Spring AI 里 ChatClient 和 ChatModel 有什么区别?什么时候该用哪个?
java·开发语言·spring·ai
moshuying11 分钟前
DeppInfer系列之 - 矩阵、注意力与 GPU:拆解 DeepGEMM 与 FlashMLA
前端·javascript·面试
小成2023032026517 分钟前
C++~05异常处理
开发语言·c++
Wang's Blog20 分钟前
Java 项目实战: 交友项目 - 圈子功能的表设计方案
java·开发语言·交友
轻舟技术A1 小时前
Rust 重写 Pi:会话启动耗时降 91%,就绪内存少 78%
开发语言·后端·rust
Jack N1 小时前
【Strapi 系列之五】前端怎么接?Next.js + Tailwind 拉取 Strapi 数据(含代码)
开发语言·前端·javascript·strapi
weedsfly1 小时前
React 不可变数据:为什么页面“明明数据变了却不更新
前端·javascript·react.js