How to make count up in flutter/dart like the digit count up in aviator game #124622
Unanswered
king2636
asked this question in
Programming Help
Replies: 1 comment 1 reply
-
Hi @king2636 , I'm not sure if that's exactly what you're looking for, but here's an example of a import 'dart:async';
import 'dart:io';
import 'dart:math';
void main() {
Counter counter = Counter();
counter.startCounter();
// Event listener for stopping and restarting the counter when pressing a key
stdin.listen((_) {
counter.stopCounter();
counter.startCounter();
});
}
class Counter {
int _counter = 0;
Timer? _timer;
final Random _random = Random();
void startCounter() {
// Set the counter to a random value between 0 and 999 (you can change it to whatever value you prefer)
_counter = _random.nextInt(1000);
_timer = Timer.periodic(Duration(milliseconds: 100), (timer) {
_counter++;
print('Counter: $_counter');
});
}
void stopCounter() {
if (_timer != null && _timer!.isActive) {
_timer?.cancel();
// Stops the counter and prints the value when pressing a key
print('Stopped at: $_counter');
}
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Body
I want to a make an auto count up in flutter, the count up starts automatically and it can stop at anytime giving a random digit value everytime it starts again. Just like the count up in aviator games. Please can anyone help.
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions