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;
    }
  }
相关推荐
行走的陀螺仪11 分钟前
uni-app + Vue3 实现折叠文本(超出省略 + 展开收起)
前端·javascript·css·uni-app·vue3
冴羽14 分钟前
JavaScript 异步循环踩坑指南
前端·javascript·node.js
ACP广源盛1392462567322 分钟前
(ACP广源盛)GSV2231---DisplayPort 1.4 MST 到 HDMI 2.0/DP/Type-C 转换器(带嵌入式 MCU)
c语言·开发语言·单片机·嵌入式硬件·音视频·mst
quant_198623 分钟前
【教程】使用加密货币行情接口 - 查询比特币实时价格
开发语言·后端·python·websocket·网络协议
熊猫_豆豆30 分钟前
Python 写一个标准版和程序员版计算器
开发语言·python·计算器
Mr.Jessy38 分钟前
Web APIs 学习第四天:DOM事件进阶
开发语言·前端·javascript·学习·ecmascript
旧时光_40 分钟前
第2章:第一个Flutter应用 —— 2.4 路由管理
flutter
醉方休42 分钟前
开发一个完整的Electron应用程序
前端·javascript·electron
studyForMokey1 小时前
【Kotlin内联函数】
android·开发语言·kotlin
小虚竹1 小时前
Rust日志系统完全指南:从log门面库到env_logger实战
开发语言·后端·rust