So I have this world map SVG from here: https://mapsvg.com/maps/world. I'm trying to copy the individual path data of the map and store them into a multi-line string. Then splitting that string by a new line.
Then for each data string, I'm converting the data string to the path object and tried to draw it, but it doesn't draw anything.
Here is the sample code:
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;
}
}
Note: The path data inside the string is of Andorra.
I'm trying to follow this answer here: https://stackoverflow.com/a/60947105/11283915. When I use the data string from this answer, it draws fine but doesn't work with the data string that I'm trying to use from the SVG.