How can I make a timer that its speed increases every minute i.e. the timer duration decreases evey 1 minute in Flutter?
You could make just one Timer.periodic with your minimum period and use a static counter in the callback to mark off multiples of that variable period. This has the advantage of being easily extended to any number of tasks running at different periods -- all driven by the same timer.
import 'dart:async';
import 'dart:math';
const int minPeriod=1;
const int maxPeriod=10;
const double decreaseRate=0.1;
int variablePeriodSeconds(int t) {
var p=decreaseRate*t;
return max(maxPeriod-p.floor(),minPeriod); // simple linear decrease
}
void main() {
int tTick=0;
int tTock=0;
final Duration minDuration = Duration(seconds: minPeriod);
Timer timer = Timer.periodic(minDuration, (Timer timer) {
print('tick');
if(tTick-tTock >= variablePeriodSeconds(tTick)){
print("TOCK!");
tTock=tTick;
}
tTick++;
});
}
The above code creates an explicit tTick counter, but the Timer object has an instance variable timer.tick that's pretty much good to go for this same purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With