I am able to run timer when the screen is loaded but I want to run that same timer in background also is there a way to do that? and I also want to convert that 30 to 30 minutes need help.
static const maxMinutes = 30;
var minute = maxMinutes;
Timer timer;
void initState() {
// TODO: implement initState
super.initState();
startTimer();
}
void dispose() {
// TODO: implement dispose
super.dispose();
minute;
startTimer();
}
void startTimer(){
timer = Timer.periodic(Duration(seconds: 1), (timer) {
if(mounted) {
setState(() {
if (minute > 0) {
minute--;
} else {
timer.cancel();
}
});
}
});
}
timer is running but i want to run the timer in background
Related
I want to create an animation where the container changes colors just the LED lights do in real life.
I this would be automatic so I dont want any 'onPressed()' working classes
I tried doing it through 'Timer' and 'AnimatedContainer()' but the timer only responds to initState value i.e. 0
Either help me setState the state of Container after every second or give me some another solution.
This is my code currently
late Timer timer;
int start = 0;
void startTimer() {
const oneSec = Duration(seconds: 1);
timer = Timer.periodic(
oneSec,
(Timer timer) {
setState(() {
start++;
});`your text`
},
);
}
#override
void initState() {
super.initState();
startTimer();
setState(() {
_changeColor();
});
}
_changeColor() {
setState(() {
if (start % 3 == 0) {
changingColor = Colors.green;
} else if (start % 3 == 1) {
changingColor = Colors.blue;
} else if (start % 3 == 2) {
changingColor = Colors.red;
}
});
}
Use setState for start won't call _changeColor, so you have to call _changeColor inside Timer.
class LEDWidget extends StatefulWidget {
const LEDWidget({super.key});
#override
State<LEDWidget> createState() => _LEDWidgetState();
}
class _LEDWidgetState extends State<LEDWidget> {
Color changingColor = Colors.green;
int start = 0;
late Timer timer;
#override
void dispose() {
timer.cancel();
super.dispose();
}
#override
void initState() {
super.initState();
timer = Timer.periodic(const Duration(seconds: 1), (_) {
start++;
_changeColor();
});
}
_changeColor() {
setState(() {
if (start % 3 == 0) {
changingColor = Colors.green;
} else if (start % 3 == 1) {
changingColor = Colors.blue;
} else if (start % 3 == 2) {
changingColor = Colors.red;
}
});
}
#override
Widget build(BuildContext context) {
return AnimatedContainer(
duration: const Duration(seconds: 1),
color: changingColor,
);
}
}
I want to check the internet on every page in my flutter app how I can do that without repeating the same code on every screen?
here is my code
bool internet = true;
#override
void initState() {
countryController.text = "+961";
Connectivity().onConnectivityChanged.listen((connectionState) {
if (connectionState == ConnectivityResult.none) {
setState(() {
internet = false;
});
// valueNotifierBook.incrementNotifier();
} else {
setState(() {
internet = true;
});
}
});
super.initState();
}
I've trying to do a multiple timer in flutter, when one ends, another one should start, for some reason, when the first timer ends, the next one doesn't begin. Here is the code
Timer timer;
//Create the sets
void startSets(){
startTimer(10,0); //10 seconds, 0 minutes
for(int i=0; i<sets;i++){
for(int j=0;j<exercises;j++){
startTimer(15,0); //15 seconds, 0 minutes
}
startTimer(12,0); //12 seconds, 0 minutes
}
}
void stopTimer(){
timer.cancel();
}
void startTimer(int seconds, int minutes){
timer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
if(seconds>0){
seconds--;
}else if(seconds==0 && minutes>0){
minutes--;
seconds = 59;
}else{
timer.cancel();
}
minText=minToString(minutes);
secText=secToString(seconds);
});
});
}
minText and secText are global variables which I use for a Text Widget
Your code doesn't match your description. You say you want one Timer to start when another one ends, but startSets start all its Timers at the same time without any waiting. If you actually want only one Timer in-flight at a time, you will need to create a queue that you process when each Timer completes.
One way to do it would be something like (this is untested code):
var timerQueue = Queue<Duration>();
Timer currentTimer;
//Create the sets
void startSets() {
timerQueue.add(Duration(seconds: 10));
for (var i = 0; i < sets; i++) {
for (var j = 0; j < exercises; j++) {
timerQueue.add(Duration(seconds: 15));
}
timerQueue.add(Duration(seconds: 12));
}
startNextTimer();
}
void stopTimer() {
currentTimer?.cancel();
currentTimer = null;
}
void startNextTimer() {
if (timerQueue.isEmpty) {
return;
}
var duration = timerQueue.removeFirst();
var seconds = duration.inSeconds % 60;
var minutes = duration.inMinutes;
assert(currentTimer == null);
currentTimer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
if (seconds > 0){
seconds--;
} else if (seconds == 0 && minutes > 0) {
minutes--;
seconds = 59;
} else {
currentTimer.cancel();
currentTimer = null;
}
minText = minToString(minutes);
secText = secToString(seconds);
if (currentTimer == null) {
startNextTimer();
}
});
});
}
declare timer variable inside startTimer method like this
void startTimer(int seconds, int minutes){
Timer timer;
...
}
so that for every time startTimer is called it will be declared
i would like to have a Timer runing once in main.dart to check user activity
#override
void initState() {
super.initState();
initPlatformState();
_fetchMasterData("a", "b");
_startActivityTimer();
}
bool _activityTimerRunning = false;
void _startActivityTimer() {
if (!_activityTimerRunning) {
Timer.periodic(Duration(seconds: 5), (timer) {
_timerTicked(timer);
setState(() {
_activityTimerRunning = true;
});
print("Timer started");
});
}
}
But initState gets called more than once, so it is not the right place to register the timer. Where should it be placed?
The code in the question has i silly mistake, the print statement is in the wrong place. Here is the working code:
#override
void initState() {
super.initState();
initPlatformState();
_fetchMasterData("a", "b");
_startActivityTimer();
}
void _startActivityTimer() {
print("Timer started");
Timer.periodic(Duration(seconds: 5), (timer) {
_timerTicked(timer);
});
}
"TimerStarted" get executed only once
Try this sample code.
int start = 5;
fName(){
Timer _timer;
const oneSecond = Duration(seconds: 5);
_timer = Timer.periodic(
oneSecond,
(Timer timer) => setState((){
if(start == 0)
timer.cancel();
else{
_activityTimerRunning = true;
start -= 1;
}
})
);
}
When you want to start you timer, call fName() method, after that timer can started. So and also you can check condition, when
if(start != 0){
...
}
I called some methods using timer..I used 4 timers. I need to call only when online. how to stop or disable timer when offile?
void connectionChanged(dynamic hasConnection) {
setState(() {
isOffline = !hasConnection;
if(!isOffline){
timer1 = Timer.periodic(Duration(seconds: 1), (Timer t) {
checkRefresh();
checkQuick(_url, tokens);
});
timer3 = Timer.periodic(Duration(seconds: 5), (Timer t) {
globals.getQuick().then((onValue) {
setState(() {
isQuick = onValue;
});
});
});
timer = Timer.periodic(Duration(seconds: 10), (Timer t) {
submitRequestSave(_url, tokens);
});
timer2 = Timer.periodic(Duration(seconds: 10), (Timer t) {
storeSync(_url, tokens);
});
}
});
}
I used dispose when navigate
#override
void dispose() {
timer1.cancel();
timer2.cancel();
timer3.cancel();
super.dispose();
}