Draw and interact with SVG in Flutter

与世无争的帅哥 提交于 2020-05-26 13:12:12

问题


We are developing an application which displays a human body, based on a SVG input. This human body is divided in several regions, think of a head, left-arm, right-arm, belly etc.

We want to highlight a region of the image when the user clicks on for example one arm. What is the best way to achieve such a thing in Flutter?

We tried to use Flare for Flutter, but this librart does not provide direct interaction with the human body being displayed.

Is there an easier way to:

  • Render the body based on a SVG (artwork might change in future developnent);
  • Detect click, e.g. GestureDetector;
  • Find pressed region based on the coordinates of the click;

Note that simple boxes will not work since parts of the image overlap. You can see the effect we want to achieve, I clicked on one arm here. Drawing some clickable box around it, will not work well.

Thanks in advance.


回答1:


I made it working by using the built_path library, which precompiles SVG paths into Path objects. We then wrapped it into a ClipPath Widget as follows:

return GestureDetector(
    onTap: () => _bodyPartTapped(part),
    child: ClipPath(
        child: Stack(children: <Widget>[
          Container(
              color: pressedBodyPart == part
                  ? Colors.blue
                  : Colors.transparent),
          CustomPaint(painter: PathPainter(path))
        ]),
        clipper: PathClipper(path)));

It will color a body part blue if it's pressed, which is working perfectly fine.

I have created a full example which can be found here: https://github.com/gi097/flutter_clickable_regions



来源:https://stackoverflow.com/questions/57489968/draw-and-interact-with-svg-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!