onTapDown method
void
onTapDown(
- TapDownDetails details,
- GlobalKey<
State< imageKey,StatefulWidget> > - dynamic callBack(),
- double imageWidth,
- double imageHeight,
Processes tap events and converts tap coordinates to image coordinates
This method handles the complex task of translating screen tap positions to coordinates relative to the original image, accounting for scaling and positioning.
Parameters:
details: Contains the tap information including global positionimageKey: Key to the widget containing the imagecallBack: Called with the adjusted position if the tap is validimageWidth: Original width of the imageimageHeight: Original height of the image
The method performs several important steps:
- Converts global tap position to local position within the image container
- Calculates the actual image size within the container (accounting for aspect ratio)
- Maps the tap coordinates to the original image dimensions
- Validates that the tap is within the image bounds
- Calls the callback with the properly adjusted position
Implementation
void onTapDown(
TapDownDetails details,
GlobalKey<State<StatefulWidget>> imageKey,
Function(Offset) callBack,
double imageWidth,
double imageHeight,
) {
// Convert global tap position to local position within the image container
final RenderBox box =
imageKey.currentContext!.findRenderObject() as RenderBox;
final Offset localPosition = box.globalToLocal(details.globalPosition);
// Get the size of the container and calculate the actual image size within it
final Size containerSize = box.size;
final Size imageSize =
ImageUtils.calculateDisplaySize(imageWidth, imageHeight, containerSize);
// Convert the tap position to coordinates relative to the original image dimensions
final Offset adjustedPosition = Offset(
(localPosition.dx / containerSize.width) * imageSize.width,
(localPosition.dy / containerSize.height) * imageSize.height,
);
// Ensure the tap is within the image bounds
if (adjustedPosition.dx < 0 ||
adjustedPosition.dx > imageWidth ||
adjustedPosition.dy < 0 ||
adjustedPosition.dy > imageHeight) {
return;
}
// Call the callback with the adjusted position
callBack.call(adjustedPosition);
}