Issue
Problem:
I built minesweeper and 350ms is too much to trigger the longPress event, I need more speed. I need implement time consider for GestureDetector, i need 200ms for detect long press event.
How my system build:
Every button use onTap and LongPress event.
My test:
I try to use this package: XGestureDetector
But it works bad because in different device not works.
I had seen but the onTap event does not work this way: StackOverFlow Answer
I need a method to solve my problem.
Solution
Using @salihgueler answer, I found the solution:
class MyWidget extends StatefulWidget {
const MyWidget({Key? key}) : super(key: key);
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
late Timer _timer;
bool isLongPressed = false;
void _startOperation() {
_timer = Timer(const Duration(milliseconds: 200), () {
print('LongPress Event');
isLongPressed= true;
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTapDown: (_) {
_startOperation();
},
onTapUp: (_) {
_timer.cancel();
if(!isLongPressed)
{
print("Is a onTap event");
}
else
{
isLongPressed = false;
}
},
child: Text(
'Hello, World!',
style: Theme.of(context).textTheme.headline4,
),
);
}
}
Answered By - EliaTolin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.