flutterlogicvibration

Flutter How to make it vibrate on the last count?


I made a counter application, the user can determine the number of their own count. if the count reaches the count limit, I want it vibrate at the last count. For example, if I specify the number 5, the smartphone will vibrate on the 5th tap. However, I have a problem, because the vibration appears on the 6th tap.

How to make it vibrate on the 5th beat?

here is my code:

int _counter = 0;
int _five = 5;


onTap: () async {
  setState(() {
    if (_counter != _five) {
      _counter++;
    } else if (_counter == _five) {
      Vibration.vibrate(duration: 500);
      _counter = _counter + 0;
    } else {}
      });
},

Solution

  • Your code need a little change like:

      onTap: () async {
        setState(() {
          if (_counter < _five) {
            _counter++;
          }
          if (_counter == _five) {
            Vibration.vibrate(duration: 500);
          } else {}
        });
      }