我有一张SVG世界Map我尝试复制Map的单个路径数据,并将它们存储到一个多行字符串中,然后将该字符串拆分为一个新行。
然后,对于每个数据字符串,我将数据字符串转换为path对象,并尝试绘制它,但它什么也没绘制。
import 'package:flutter/material.dart';
import 'package:path_drawing/path_drawing.dart';
import 'package:touchable/touchable.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: CanvasTouchDetector(
gesturesToOverride: const [GestureType.onTapUp],
builder: (context) {
return CustomPaint(
size: const Size(double.infinity, double.infinity),
painter: MyPainter(context),
);
},
),
);
}
}
class MyPainter extends CustomPainter {
final BuildContext context;
MyPainter(this.context);
@override
void paint(Canvas canvas, Size size) {
TouchyCanvas touchyCanvas = TouchyCanvas(context, canvas);
final List<String> paths =
'''m 479.68275,331.6274 -0.077,0.025 -0.258,0.155 -0.147,0.054 -0.134,0.027 -0.105,-0.011 -0.058,-0.091 0.006,-0.139 -0.024,-0.124 -0.02,-0.067 0.038,-0.181 0.086,-0.097 0.119,-0.08 0.188,0.029 0.398,0.116 0.083,0.109 10e-4,0.072 -0.073,0.119 z'''
.split('\n');
// drawing the paths
for (int i = 0; i < paths.length; i++) {
Path path = parseSvgPathData(paths[i]);
touchyCanvas.drawPath(
path,
Paint()
..color = Colors.red
..strokeWidth = 2,
onTapUp: (details) {
print('clicked');
},
);
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
注意:字符串中的路径数据是安道尔的。
我试着去理解这个答案:https://stackoverflow.com/a/60947105/11283915。当我使用这个答案中的数据字符串时,它绘制得很好,但不能与我尝试使用的SVG中的数据字符串一起使用。
1条答案
按热度按时间bttbmeg01#
将自定义绘图器的大小设置为与SVG的视框相同会使路径显示在绘图器上。例如,我有一个
viewbox="0 0 2000 857"
的SVG,所以我将自定义绘图器大小设置为Size(2000, MediaQuery.of(context).size.hieght)
,将高度设置为设备高度,只是因为我的设备高度非常接近视框中定义的高度。下面的代码可以工作:
要查看绘制的路径,我只需要滚动屏幕。如果你想限制SVG绘制的区域,请按照以下步骤操作:https://stackoverflow.com/a/60947105/11283915