I am trying to implement the button, where on long press you need to hold a breath for 10 seconds, and for user I want to show how many seconds left, but with the current implementation number is static on long press and I cannot get why, because function is called inside onLongPress and cancelled onLongPressEnd.
late Timer ?timer;
int _counter = 10;
void startTimer() {
const oneSec = const Duration(seconds: 10);
timer = new Timer.periodic(
oneSec,
(Timer ?timer) {
if (_counter == 0) {
setState(() {
timer?.cancel();
});
} else {
setState(() {
_counter--;
});
}
},
);
OnLongPress implementation
onLongPress: () {
startTimer();
setState(() {
_counter;}
onLongPressEnd: (details) {
timer?.cancel();
setState(() {
_isLongPressActivated = false;
},
Thanks a lot for any help on that
2
Answers
There may be more than one issue with the code you posted, but I took your code and here’s a working ticker based on the getting-started app:
GestureDetecture
withonTapDown
andonTapUp
are the right callbacks you need to implement.