当前位置: 首页 > news >正文

Flutter SVG图片Demo

欢迎大家加入开源鸿蒙跨平台社区

Flutter SVG 图片 Demo

本文介绍在 Flutter(鸿蒙跨平台)项目中不依赖任何第三方库,使用纯 Dart +CustomPainter实现 SVG 图形渲染、属性编辑、动画播放的核心要点。


一、方案选择:纯 Dart 替代 flutter_svg

flutter_svg等主流 SVG 插件在鸿蒙平台存在兼容问题,因此采用CustomPainter直接绘制矢量图形,实现零依赖、完全跨平台。

class_ShapePainterextendsCustomPainter{finalvoidFunction(Canvas,Size)draw;_ShapePainter(this.draw);@overridevoidpaint(Canvascanvas,Sizesize)=>draw(canvas,size);@overrideboolshouldRepaint(_)=>false;}

将绘制逻辑作为函数传入,实现高度复用。


二、整体结构

使用IndexedStack+NavigationBar实现三 Tab 布局:

body:IndexedStack(index:_tabIndex,children:const[_GalleryTab(),_EditorTab(),_AnimTab()],),

三、常用 SVG 图形绘制

星形(多边形路径)

通过奇偶半径交替计算顶点绘制星形:

Path_starPath(double cx,double cy,double outerR,double innerR,int points){finalpath=Path();for(int i=0;i<points*2;i++){finalr=i.isEven?outerR:innerR;finalangle=pi*i/points-pi/2;finalx=cx+r*cos(angle);finaly=cy+r*sin(angle);i==0?path.moveTo(x,y):path.lineTo(x,y);}returnpath..close();}

心形(贝塞尔曲线)

使用cubicTo三次贝塞尔曲线构造心形轮廓:

path.moveTo(w*0.5,h*0.85);path.cubicTo(w*0.1,h*0.6,w*0.0,h*0.3,w*0.25,h*0.2);path.cubicTo(w*0.4,h*0.1,w*0.5,h*0.2,w*0.5,h*0.3);// 对称右侧...

螺旋线(参数方程)

阿基米德螺旋:半径随角度线性增长:

for(int i=0;i<=steps;i++){finalt=i/steps;finalangle=t*turns*2*pi;finalr=t*maxRadius;// 半径线性增长finalx=cx+r*cos(angle);finaly=cy+r*sin(angle);i==0?path.moveTo(x,y):path.lineTo(x,y);}

四、SVG 属性编辑器

通过Paint对象的参数实时控制填充、描边、透明度:

finalfill=Paint()..color=fillColor..style=PaintingStyle.fill;finalstroke=Paint()..color=strokeColor..style=PaintingStyle.stroke..strokeWidth=strokeWidth;

配合Transform.rotate+Transform.scale+Opacity实现旋转、缩放、透明度的实时预览:

Opacity(opacity:_opacity,child:Transform.rotate(angle:_rotation*pi/180,child:Transform.scale(scale:_scale,child:CustomPaint(...)),),),

五、SVG 动画

使用AnimationController+ 多个Tween驱动旋转、缩放、颜色动画:

_ctrl=AnimationController(vsync:this,duration:constDuration(seconds:2))..repeat(reverse:true);_rotateAnim=Tween(begin:0.0,end:2*pi).animate(_ctrl);_scaleAnim=Tween(begin:0.5,end:1.5).animate(_ctrl);_colorAnim=ColorTween(begin:Colors.blue,end:Colors.orange).animate(_ctrl);

AnimatedBuilder监听动画值,按模式选择性应用变换:

AnimatedBuilder(animation:_ctrl,builder:(_,__){finalangle=(_animMode==0||_animMode==3)?_rotateAnim.value:0.0;finalscale=(_animMode==1||_animMode==3)?_scaleAnim.value:1.0;finalcolor=(_animMode==2||_animMode==3)?_colorAnim.value!:Colors.blue;returnTransform.rotate(angle:angle,child:Transform.scale(scale:scale,...));},),

六、花瓣动画绘制

CustomPainter中通过progress参数驱动花瓣旋转和圆弧进度:

for(int i=0;i<petalCount;i++){finalangle=2*pi*i/petalCount+progress*2*pi;// 随进度旋转canvas.drawCircle(Offset(cx+r*cos(angle),cy+r*sin(angle)),r*0.35,paint);}// 外圆弧进度canvas.drawArc(rect,-pi/2,2*pi*progress,false,arcPaint);

总结

模块核心技术
图形绘制CustomPainter+CanvasAPI
路径构造Path.cubicTo/lineTo/ 参数方程
属性编辑Paint参数 +Transform+Opacity
动画驱动AnimationController+Tween+AnimatedBuilder
平台兼容纯 Dart 实现,无需第三方插件,完全支持鸿蒙
http://www.jsqmd.com/news/453513/

相关文章:

  • 编译器优化屏障使用
  • 基于SpringBoot+Vue的船舶监造系统管理系统设计与实现【Java+MySQL+MyBatis完整源码】
  • 【ArcGIS技巧】表格批量转图片(emf格式)方便相对路径索引表格
  • Qwen3-ASR-0.6B语音识别实测:轻量级模型,专业级效果,小白也能用
  • redis具体情况介绍
  • 云容笔谈微信小程序前端开发实战:打造个人AI画师工具
  • HeyGem数字人视频生成系统批量版:5分钟快速部署,新手也能轻松上手
  • L1-020 帅到没朋友(分数20)
  • 索引和事务
  • 一键部署梦幻动漫魔法工坊:快速搭建你的二次元创作平台
  • 探寻2026年贵阳诚信的网络营销培训学校,怎么选择更合适 - myqiye
  • 聊聊江苏宇灿智能装备技术水平怎么样,其管道加热器值得推荐吗 - 工业推荐榜
  • 春联生成模型-中文-base内存优化:解决大并发下的显存溢出问题
  • Qwen2-VL-2B-Instruct保姆级教程:Pillow+Sentence-Transformers环境配置全步骤
  • AWPortrait-Z快速入门:3步搞定你的第一张AI肖像照
  • RVC语音变声器教育应用:语言学习发音纠正与语音模仿训练
  • 分布式存储系统设计
  • 释放创意:用MiniCPM-o-4.5为短视频脚本生成分镜与文案
  • 2026年口碑好的家电展会推荐,专业家电展会服务企业全盘点 - mypinpai
  • ComfyUI Qwen人脸生成图像实战:用AI为老照片生成清晰全身影像
  • Qwen3-TTS-VoiceDesign一键部署:start_demo.sh脚本解析与自定义端口修改方法
  • 2026年南昌性价比高的装修公司推荐,探讨丛一楼装饰设计水平与反馈 - 工业设备
  • 造相-Z-Image保姆级教程:RTX 4090专属,5分钟本地部署文生图系统
  • Qwen1.5-1.8B GPTQ开发环境搭建:IntelliJ IDEA集成指南
  • 讲讲全国高强丝定制专家,中祥线业推荐选吗? - 工业品牌热点
  • 音频处理新神器:Qwen3-TTS-Tokenizer-12Hz快速上手指南
  • 2026最新论文降重教程:免费降AI率指令与3款工具实测数据对比
  • Qwen3-ASR-0.6B语音识别部署案例:政务热线录音智能归档系统
  • 造相-Z-Image与IoT设备集成:智能家居场景生成系统
  • 梳理2026年清水混凝土装饰企业选购攻略,资质售后双优企业盘点 - 工业品网