深圳市建设厅,手机网站优化怎么做,西安网站建设推广公司,广西建设学院网站Flutter 实现一个容器内部元素可平移、缩放和旋转等功能#xff08;九#xff09;
Flutter: 3.35.7
今天的功能完成那基本功能就完成了#xff0c;所以给出github链接#xff1a;
https://github.com/yhtqw/FrontEndDemo/tree/main/flutter_demo/lib/pages/transform_use/w…Flutter 实现一个容器内部元素可平移、缩放和旋转等功能九Flutter: 3.35.7今天的功能完成那基本功能就完成了所以给出github链接https://github.com/yhtqw/FrontEndDemo/tree/main/flutter_demo/lib/pages/transform_use/widgets/multiple_transform前面我们简单实现了图片元素和文本元素的新增优先实现的功能很多地方还可以优化。今天我来讨论一下文本元素的拖动缩放。对于文本元素文本元素的高度是由文本自身的样式来决定的所以我们限制文本元素只能缩放宽度高度自动计算。这样就会对缩放区域进行元素类型判断如果是文本元素那么只缩放宽度高度自适应如果是其他元素则同上对宽度高度进行缩放。既然功能又区别那么icon也要有区别那么之前的元素操作区域得做出更改新增这种特殊功能区分元素类型对应图标不一样的配置classResponseAreaModel{// 其他省略.../// 元素类型不同展示不同的操作iconfinalMapString,String?iconConfig;// 其他省略...}classConstantsConfig{// 其他省略...staticfinalListResponseAreaModelbaseAreaList[// 其他省略...// 缩放ResponseAreaModel(areaWidth:20,areaHeight:20,xRatio:1,yRatio:1,status:ElementStatus.scale.value,icon:assets/images/icon_scale.png,trigger:TriggerMethod.move,iconConfig:{ElementType.textType.type:assets/images/icon_scale_text.png,},),// 其他省略...],// 其他省略...}// 其他省略...// 如果选中则展示操作区域if(selected)...areaList.map((item)Positioned(top:elementItem.elementHeight*item.yRatio-item.areaHeight/2,left:elementItem.elementWidth*item.xRatio-item.areaWidth/2,child:Container(width:item.areaWidth,height:item.areaHeight,alignment:Alignment.center,decoration:BoxDecoration(color:Colors.blueAccent,borderRadius:BorderRadius.circular(item.areaWidth/2),),child:Image.asset(// 判断对应类型的图片是否单独存在item.iconConfig?[elementItem.type]??item.icon,width:item.areaWidth-ConstantsConfig.areaIconMargin,height:item.areaHeight-ConstantsConfig.areaIconMargin,fit:BoxFit.scaleDown,color:Colors.white,),),)),运行效果这样我们就通过元素状态区分了特殊功能的图标展示现在就是功能的实现了/// 处理元素缩放////// 通过移动点坐标[x]和[y]与按下的初始坐标void_onScale({required double x,required double y}){if(_currentElement?.typeElementType.textType.type){_onScaleText(x:x,y:y);}else{_onScaleBase(x:x,y:y);}}/// 抽取获取缩放需要的基础参数(double,double,double,double,double)_getScaleParams({required double x,required double y}){finaldouble oWidth_temporary!.width;finaldouble oHeight_temporary!.height;finaldouble oX_temporary!.x;finaldouble oY_temporary!.y;finaldouble resizeRatio_calcResizeRatio(x:x,y:y);return(oWidth,oHeight,oX,oY,resizeRatio);}/// 处理非文本元素的缩放void_onScaleBase({required double x,required double y}){if(_currentElementnull||_temporarynull)return;final(oWidth,oHeight,oX,oY,resizeRatio)_getScaleParams(x:x,y:y);double newWoWidth*resizeRatio;double newHoHeight*resizeRatio;finaldouble minSizeConstantsConfig.minSize;// 以短边为基准来计算最小宽高if(oWidthoHeightnewWminSize){newWminSize;newHminSize*oHeight/oWidth;}elseif(oHeightoWidthnewHminSize){newHminSize;newWminSize*oWidth/oHeight;}// 以长边为基准来计算最大宽高if(oWidthoHeightnewW_transformWidth){newW_transformWidth;newH_transformWidth*oHeight/oWidth;}elseif(oHeightoWidthnewH_transformHeight){newH_transformHeight;newW_transformHeight*oWidth/oHeight;}if(newW_currentElement?.elementWidthnewH_currentElement?.elementHeight){return;}_currentElement_currentElement?.copyWith(elementWidth:newW,elementHeight:newH,x:oX-(newW-oWidth)/2,y:oY-(newH-oHeight)/2,);_onChange();}/// 文本元素的缩放void_onScaleText({required double x,required double y}){if(_currentElementnull||_temporarynull)return;final(oWidth,oHeight,oX,oY,resizeRatio)_getScaleParams(x:x,y:y);double newWoWidth*resizeRatio;finaldouble minSizeConstantsConfig.minSize;// 以短边为基准来计算最小宽高if(oWidthoHeightnewWminSize){newWminSize;}// 以长边为基准来计算最大宽高if(oWidthoHeightnewW_transformWidth){newW_transformWidth;}finalTextStylestyle_getTextStyle(_currentElement!.textOptions!);final(tempWidth,tempHeight)TransformUtils.calculateTextSize(text:_currentElement!.textOptions!.text,style:style,maxWidth:newW,);_currentElement_currentElement?.copyWith(elementWidth:newW,elementHeight:tempHeight,x:oX-(newW-oWidth)/2,y:oY-(tempHeight-oHeight)/2,);_onChange();}运行效果这样我们就实现了文本元素的拉伸效果。可以看到随着功能的增加可能就需要对之前的逻辑做更改不过因为抽取了配置很多地方就只需要加入少量的代码即可实现部分功能。基础的功能差不多就完成了接下来主功能就还有一个那就是保存我们不知道这个功能是否还存在编辑如果不存在直接将该结构转成图片返回即可所以我们保存就直接将数据和图片都传递过去即可。一般这种大量数据存后端都是JSON字符串所以我们直接转成json字符串存储Futurevoid_onSave()async{if(_isLoading)return;_isLoadingtrue;if(_currentElement!null){setState((){_currentElementnull;});}if(_currentElementnull){finalStringimagePathawait_getImagePath();finalListMapString,dynamictempStringList_elementList.map((item)ElementModel.toJson(item)).toList();widget.onSave(imgSrc:imagePath,data:jsonEncode(tempStringList));_isLoadingfalse;}else{_isLoadingfalse;_onSave();}}运行效果这样我们就简单实现了图片的生成并且将数据转换成JSON字符串传递给后端保存后续后端返回这个字符串再还原就可以进行编辑。这样就简单实现了基础的功能后续就实现辅助的功能。感兴趣的也可以关注我的微信公众号【前端学习小营地】不定时会分享一些小功能好了今天的分享到此结束感谢阅读拜拜