Flutter Timer.periodic register/run once - flutter

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){
...
}

Related

How to add continuously change color of container in flutter between RGB values

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,
);
}
}

internet listen on every screen flutter

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();
}

is there a way to run timer in background?

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

Timer.periodic flutter,

#override
void initState() {
super.initState();
bool isLastPage = (_currentPage.round() == sliderArrayList.length - 1 );
//Trying to build automatic scrollable
Timer.periodic(Duration(seconds: 5), (Timer timer) {
if (_pageController.hasClients && !isLastPage) {
_pageController.nextPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeIn);}
if(isLastPage){
timer.cancel();
}
});
}
I am trying to build a periodic timer which scrolls pages in pageview, flutter after every 5 seconds, however it keeps on scrolling even after the last page. I have tried to implement the above method if(isLastPage){timer.cancel();} and it doesnt work.
You've to evaluate that expression everytime timer ticks. Also, Put the isLastPage check before the hasClients check.
Timer.periodic(Duration(seconds: 5), (Timer timer) {
final isLastPage = _currentPage.round() == sliderArrayList.length - 1;
if (isLastPage) {
timer.cancel();
return;
}
if (_pageController.hasClients) {
_pageController.nextPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeIn);
}
});

how to stop/dispose timer when offline?

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();
}