flutter - Flutter 如何在 onTap 后使用 Gesture Detector 更改 SVG
问题描述
想要实现与图像相同的功能:
我正在使用这个包: https ://pub.dev/packages/flutter_svg/example
即使我能够在 IconButton 中执行此操作,我也需要在 GestureDetector 中,而不是在 IconButton 中,所以为了清楚起见,需要帮助如何将 SVG 实现为 GestureDetector 而不是 IconButton!
我能够在屏幕上渲染 SVG,但无法弄清楚如何在 GestureDetector 中实现它,在屏幕上渲染 SVG,我这样做是这样的:
Expanded svgTest({double blackKeyWidth}) {
return Expanded(
child: Container(
child: IconButton(
icon: SvgPicture.asset(
'assets/images/1.svg',
fit: BoxFit.fill,
allowDrawingOutsideViewBox: true,
width: 500,
),
onPressed: null //do something,
),
),
);
}
无法弄清楚如何在 GestureDetector 中实现 SVG,它无法进入,因为 Image 和 Gesture Detector 不接受图标,或者我做得不对。
感谢
解决方案
如果要更改onTap
方法上的图像,iconButton
则必须使用更改in方法的路径image
onTap
setState
String imagePath = "assets/images/1.svg";
Expanded svgTest({double blackKeyWidth}) {
return Expanded(
child: Container(
child: IconButton(
icon: SvgPicture.asset(
'${imagePath}',
fit: BoxFit.fill,
allowDrawingOutsideViewBox: true,
width: 500,
),
onPressed: (){
setState(() {
imagePath = "assets/images/2.svg";
});
},
),
),
);
}
编辑:(对于手势检测器,您可以简单地将 SVG 包裹在其中)
将 the iconbutton
替换为 the GestureDetector
并添加它的onTap
andonTapUp
方法。
GestureDetector(
onTap: () {
setState(() {
imagePath = "assets/images/2.svg";
});
},
onTapUp: (TapUpDetails tapUpDetails) {
setState(() {
imagePath = "assets/images/1.svg";
});
},
child: SvgPicture.asset(
'${imagePath}',
//fit: BoxFit.fill,
//allowDrawingOutsideViewBox: true,
//width: 500,
),
),
推荐阅读
- javascript - 获取当前在 ngFor 中运行的元素
- node.js - 在创建新的 Angular 项目时包含 .npmrc
- ocr - 有没有办法将(所有)区域/框自动导出为 jpg/png/图像?
- powershell - 标准 Azure 搜索服务的 REST API 速率限制是多少?
- php - 如何增加 cookie 的最大大小?
- python - Python中的可变和不可变
- python - Django REST 框架:类型对象 X 没有属性“get_extra_actions”
- django - 是否可以在 ArrayModelField 中嵌入 ArrayModelField?
- java - 使用 bouncycastle 在 java 中实现 gost 28147-89 加密和解密算法
- javascript - 来自外部 js 文件的 D3 SVG