Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing onTap event #1

Open
ReinisSprogis opened this issue Jul 14, 2023 · 3 comments
Open

Implementing onTap event #1

ReinisSprogis opened this issue Jul 14, 2023 · 3 comments

Comments

@ReinisSprogis
Copy link

I was trying to implement onTap callback but with no luck. If wrapping child with GestureDetector, it's not working. Would be awesome if could have onTap callback that returns SelectedSide when either of sides are tapped.

@ElRojo7u7
Copy link
Owner

ElRojo7u7 commented Jul 18, 2023

You were trying to implement onTap callback (?
I didn't have that in mind but it would be a nice thing to have
Just to be sure, you mean something like this? (srry for the quality :'u)

output

If that's the case it has a simple solution and another with more odds to have more actions rather than only onTap

You're right, wrapping a face into a GestureDetector it doesn't detect the actions, that's becase the widgets you send as faces are then wrapped into another GestureDetector (lib/src/widgets/cubixd.widget.dart lines 183-190)

  1. Adding a onTap callback into the GestureDetector I mentioned before
  2. Implement a RawGestureDetector. Here's more info

@ReinisSprogis
Copy link
Author

Hi. Thanks for replay!
That's exactly what I tried to do! However, not exactly sure if in your example it returns side that is tapped or how many times it is tapped?
I added onTap in the same place, but it wasn't able to get it working correctly.
If I remember correctly, I was able to get the same result.
Honestly, I struggled for few hours and tried many things without luck. Ended up thinking, that I need some kind of ray caster to detect what side is tapped.

I followed your patterns and added
typedef OnTap = void Function(SelectedSide opt);

/// The callback that triggers when the cubix is moving
final VoidCallback? onPanUpdate;

///Returns the [SelectedSide] tapped.
final OnTap? onTap;

then in build
child: GestureDetector( onPanUpdate: widget.onSelected != null ? _onDraging : null, onTap: () => widget.onTap != null ? _onTap : null, child: Container( color: Colors.transparent, height: _height * 1.2, width: _width * 1.2, ), ),

And struggled to create _onTap function that would return correct SelectedSide enum.
I think some math needed to be done to calculate from local position to a position of tap and detect what face is there?

I also tried to remove that GestureDetector and add one to each face, but that didn't work either.

I am using your widget as view cube, to display how my graphics are orientated, working fine, just wanted to add onTap functionality that will rotate graphics to that side tapped on CubixD.

@ElRojo7u7
Copy link
Owner

I see. My last comment doesn't help at all. The problem here is: In order to move the cube there is a transparent container just in front the cube, so if you tab any face of it, you're actually tapping the transparent container (lib/src/widgets/cubixd.widget.dart lines 183-190) That's the best way i found to move it, because if I move the GestureDetector to wrap the cube instead (the SizedBox just above) the cube doesn't moved correctly.

Adding behavior: HitTestBehavior.translucent to the GestureDetector doesn't help at all, i think it is because the faces are into another Stack than the GestureDetector of the transparent Container. Removing the transparent Container solves it, but removing it is not an option :'u

Using a GestureDetector as part of a face like

...
top: GestureDetector(
      onTap: () => print(":p")
      child: Container(
...
   )
)
...

Doesn't work, the GestureDetector needs to be wrapping the Transform widget (but by doing this the position loses precision)

Given this situation I think the same, meaby some math is needed in order to get the position and get the SelectedSide.

I'll give it a try later :'u

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants