admin管理员组

文章数量:1332353

I am working on a Flutter application where I have an image (like a car), and I want to detect when a user taps on specific parts of the image. Upon tapping a part, it should change color to indicate selection, similar to this example:

I am currently using an SVG image, but I’m open to other formats if necessary. I need the solution to:

  • Detect which part of the image was tapped.
  • Change the color of the tapped part while leaving other parts unaffected.
  • Allow for dynamic resizing of the image without breaking the tap detection.

What is the best way to achieve this in Flutter? Should I use custom paths, hotspots, or something else? Any examples or suggestions would be greatly appreciated!

I am working on a Flutter application where I have an image (like a car), and I want to detect when a user taps on specific parts of the image. Upon tapping a part, it should change color to indicate selection, similar to this example:

I am currently using an SVG image, but I’m open to other formats if necessary. I need the solution to:

  • Detect which part of the image was tapped.
  • Change the color of the tapped part while leaving other parts unaffected.
  • Allow for dynamic resizing of the image without breaking the tap detection.

What is the best way to achieve this in Flutter? Should I use custom paths, hotspots, or something else? Any examples or suggestions would be greatly appreciated!

Share Improve this question edited Nov 21, 2024 at 7:33 Christoph Rackwitz 15.6k5 gold badges39 silver badges51 bronze badges asked Nov 20, 2024 at 23:18 nassim Solimannassim Soliman 292 bronze badges 3
  • 1 This question is similar to: Hit-testing SVG shapes?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Christoph Rackwitz Commented Nov 21, 2024 at 7:34
  • the first answer below looks awfully complicated for such a simple task. – Christoph Rackwitz Commented Nov 21, 2024 at 7:36
  • I changed my answer, I think that this another way will be more easy for you. – Alexandre B. Commented Nov 21, 2024 at 18:16
Add a comment  | 

1 Answer 1

Reset to default 1

Easy way:

You can have two images, one is the original that will be showed to the user and one is invisible. This invisible image will have the same size but instead a beautiful image with details, will have only a solid colors. Differents colors for each part.

Using a package like image_pixels_4 you can easily get the color of the pixel where you clicked.

           ImagePixels(
              imageProvider: imageProvider,
              builder: (context, img) =>
                  double x= xPos; 
                  double y= yPos; 
                  Text(
                     "Pixel color is: ${img.pixelColorAt(x, y)}.");
              );

Now is easy, with a Swift Case you can call some method like setList(SelectedPart selected) and feed a List for instance.


Another way(only interesting if you need also change colors like creating sub groups):

You will need to start with Stack.

Think like in layers.

First layer is to put your image there.

Second you can create one Container(MainArea) that will have the same size of the all image. Use Positioned to set this Container over the image.

Inside this MainArea container in child:, create Columns and Rows and create anothers Containers(EachParts) to fill the area of each parts.

Use Flex or Expanded to help you when you resize the MainArea and keep all more or less also scalable. Need more research here.

In all those EachParts containers you can set the color:Colors.transparent and wrap it with a GestureDetector and get when tap happens.

I guess that could be a good start.

Good luck!

本文标签: dartHow to Detect and Highlight Tapped Parts of an Image in FlutterStack Overflow