Flutter: Recognise touch immediately - flutter

Is there a way to recognize a touch input immediately in Flutter? Normal buttons of course don't work since they trigger on lift off. The closest thing I could find was using onTapDown by the GestureDetector widget. But this only triggers after a short delay. Thank you for any advice!

You could try wrapping your widget with the Listener widget and use the onPointerDown property.
e.g.
Listener(
child: Scaffold(),
onPointerDown: (_) {},
)

Related

How to make a dragable widget scalable?

In Flutter, how do you make a widget scaleable and dragable at the same time?
A GestureDetector, for some reason, behaves a little strange. For example:
return GestureDetector(
onHorizontalDragUpdate: onHorizontalScroll,
onScaleUpdate: onScale,
behavior: HitTestBehavior.opaque,
child: CustomPaint(
painter: SomeCustomPainter( /* some parameters */ ),
),
);
The above GestureDetector is placed somewhere deep down inside a SingleChildScrollView.
Problem is that onHorizontalScroll triggers most of the time when onScale is supposed to trigger.
In which case (about 9 out of 10 times) the widget doesn't scale.
The only way to reliably make the widget scalable is by removing onHorizontalDargUpate, but then it can't be dragged any more...
So the question now is, how do you make a widget dragable and scalable at the same time? Any advise is very much appreciated. Thank you.
I suggest using the Draggable widget and DragTaregt to drop it in. it is useful and better than creating draggable from scratch. check this link

Flutter: Allow Parent and Child to respond to Drag Gesture

In Flutter, is there a way to allow both a parent and a child to respond to a Drag Gesture and toggle between which one is receiving it mid-drag.
I have a Widget that updates its size using onVerticalDragUpdate from a GestureDetector Widget. Its child is a ListView wrapped in IgnorePointer. When the parent is the correct size I set the state to prevent the parent from responding to gestures and set ignore to false for the child to allow it to scroll.
Whilst this works, the user has to lift their finger from the screen and scroll again for the child to begin scrolling. Is there a way to achieve this with the same gesture so that if the user is still dragging and the parent reaches the correct size, the child begins to scroll instead all without having to lift the finger.
Here is a simplified example.
final ignorePointer = useState<bool>(true); // hook state
double desiredSize = 100;
GestureDetector(
onVerticalDragUpdate: ignorePointer ? (details){
if((details.globalPosition.dy / desiredSize) >= 1){
ignorePointer.value = false;
}
} : null,
child: IgnorePointer(
ignoring: ignorePointer.value,
child: ListView(
children:[for(int i = 0; i < 100; i++) Text('Boo $i')]
),
),
);
The way that Gestures and other pointer events work is that the Gesture furthest down the stack that can receive an input, will receive an input. Whilst IgnorePointer can prevent a child element receiving the input, the GestureDetector won't 'let go' of its 'win' as the receiving input until that gesture comes to an end. So changing the state of IgnorePointer mid-drag will change the state but not undo the win of GestureDetector as the receiving input until that Gesture ends.
What is needed here is a way for both the ListView and the GestureDetector to receive the user input. Then with logic decide which one (or both) is going to act on the user input.
Therefore you need a Widget that can register the user input without blocking other Widgets also receiving user input.
There's a Widget for that: Listener.
Behind the scenes GestureDetector engages in a little battle in something called the GestureArena that decides which Gesture wins in any battle of multiple gestures or multiple elements that could receive gestures. Listener does not engage in this battle.
So using Listener is the answer as you will now be able to receive all events received by any user input be it pointerDown, pointerUp, pointerMove etc.. etc.. regardless of anything else (like a ListView) that may also be directly responding to user inputs. You then need to decide what you do with those events.
Listener(
onPointerMove: (e) => // your logic
child: // your scrollable widget
)

Press button on every swipe | Flutter

I've made a simple piano app in Flutter where there are multiple adjacent buttons, each playing a different note/sound. I want the button's onPressed: () method (or the code inside it) to work whenever the user's finger passes over that exact button's area (same for all buttons). The method should work each time even if the finger passes over the button more than once in a single swipe.
I've looked into the GestureDetector class but I don't think it quite achieves the multiple presses in a single swipe.
Any ideas how can I make this possible?
I'd suggest you to try using the Listener widget.
By setting PointerMoveEventListener you'll be able to perform different actions for each position of the pointer, but you'll have to map the position and sizes of each button.
I just figured this out.
Just wrap your key in a MouseRegion and call the onTap function on onEnter.
MouseRegion(
onEnter: (event) {
if(event.down){
onTap!();
}
},
child: InkWell(
borderRadius: _borderRadius,
highlightColor: Colors.grey,
onTapDown: onTap!;
),
)
This doesn't trigger the button or inkwell animation though. But this can be solved with a custom button.

How to add suggetionbox in web when cursor is on button

I want to show suggestion when cursor is on button or on some widget in flutter web
I share example how I want to show suggestion box but I don't know how to add suggestion box in flutter
You need to wrap widget with Tooltip see example.
flutter tooltip example
Try below code hope its help to you. Used Tooltip Widget
Refer Tooltip here
Test below code here
Tooltip(
message: "Managers",
child: ElevatedButton(
onPressed: () {},
child: Text('OK'),
),
),
Result Screen->
In addition to all the tooltip related recommendations, if they don't quite meet your requirements, there's OverlayEntry It allows to use any widget, but is not super easy to handle, so there's a couple packages out there that do some work with Overlays for you that you might consider.
https://pub.dev/packages/flutter_portal
https://pub.dev/packages/modals
To show overlay entry when hovering over some widget, wrap it with MouseRegion and use its onEnter and onExit methods to show and hide overlay

InkWell effect starts delayed if using onDoubleTap; want to trigger that as soon as the widget gets touched

If you are using onTap & onDoubleTap side by side with an InkWell, then a single tap gets delayed (300ms). I know the time is needed for the recognition of a double tap but also the effect is delayed, and that's a bad user interaction feeling.
What I've tried:
I found out, that the InkWell effect is startet as soon as any tap event callback gets called. If I use onTap alone, the callback and the effect is started instantly on the first touch; if I use onTap and onDoubleTap, the effect gets delayed.
I also tried using onTapDown, but this also gets delayed (possilbe Bug?)
child: InkWell(
onTap: () { print("Tap"); }, // gets delayed 300ms
onDoubleTap: () { print("Double Tap"); },
onTapDown: (x) { print("Tap Down"); } // gets delayed 300ms
}
So my question:
Is it anyway possible to change the InkWell (GestureDetector) to start the effect instantly. I think the solution could be, to change the onTapDown behavior; this should be called immediately if the user touches the widget.
I've now found a simple solution. I am only using the onTap function of the InkWell and have written the onDoubleTap algorithm my self. So the splash effect runs immediately because the original onDoubleTap isn't used. The tap function itself gets delayed (as it also would have been by using the original onDoubleTap function).
Pup.dev package
I've written a pub.dev package called InkWellSplash. Additional to the splash feature, you can now adjust the maximal time between two taps to recognize a double tap.
I'm pretty sure this package comes in handy and is useful for several developer
Links
Pub.dev - InklWellSplash
GitHub - InkWellSplash
Interactive Example