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;
    }
  }
相关推荐
张忠琳15 分钟前
【Go 1.26.4】(Part 2) Go 1.26.4 超深度分析 — Runtime GMP 调度器 (proc.go + runtime2.go)
开发语言·golang
如果超人不会飞25 分钟前
WebMCP:当浏览器学会和 AI「说人话」,你的网页就成了智能体的游乐场
javascript
spmcor33 分钟前
Flutter 学习笔记 (3):布局初探 —— Row、Column、Stack 与 Container
flutter
整点可乐34 分钟前
cesium实现全景图加载
javascript·cesium
阿坤带你走近大数据42 分钟前
java中泛型不能用基础数据类型
java·开发语言
weixin_3077791344 分钟前
从脚本执行到智能体协作:AI辅助测试能力的范式重构
运维·开发语言·人工智能·算法·测试用例
云絮.1 小时前
增删改查操作
java·开发语言
themingyi1 小时前
Abaqus2024安装python包pandas
开发语言·python·pandas
阿正的梦工坊1 小时前
【Rust】19-FFI、ABI 与跨语言边界设计
开发语言·后端·rust
殇淋狱陌1 小时前
Python列表知识思维导图
开发语言·python·学习