*facing error in timer functionality in flutter project *please any one can help me to get rid of this timer functionality error
import 'dart:async';
import 'package:flutter/material.dart';
class StopwatchHome extends StatefulWidget {
StopwatchHome({Key? key}) : super(key: key);
@override
State<StopwatchHome> createState() => _StopwatchHomeState();
}
class _StopwatchHomeState extends State<StopwatchHome> {
int secounds=0, minutes=0, hours=0;
String digitsecounds="00", digitminutes ="00", digithours="00";
Timer? timer;
bool started= false;
List laps=[];
void stop(){
timer!.cancel();
setState(() {
started=false;
});
}
void reset(){
timer!.cancel();
setState(() {
secounds=0;
minutes=0;
hours=0;
digitsecounds="00";
digitminutes="00";
digithours="00";
started=false;
});
}
void addlaps(){
String lap="$digithours:$digitminutes:$digitsecounds";
setState(() {
laps.add(lap);
});
}
void start(){
started=true;
timer=Timer.periodic(Duration(seconds: 1), (timer) {
int localSecounds=secounds 1;
int localMinutes= minutes=0;
int localHours= hours;
if (localSecounds>59) {
if (localMinutes>59) {
localHours ;
localMinutes==0;
}else{localMinutes ;localSecounds==0;}
}
setState(() {
secounds= localSecounds;
minutes= localMinutes;
hours= localHours;
digitsecounds=(secounds>=10)?"$secounds":"0$secounds";
digithours=(hours>=10)?"$hours":"0$hours";
digitminutes=(minutes>=10)?"$minutes":"0$minutes";
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor:
// const Color(0xff213a20),
Color.fromARGB(255, 138, 11, 87),
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(18),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Text("Stopwatch",
style: TextStyle(color: Colors.white,
fontWeight: FontWeight.bold,fontSize: 40,
fontStyle: FontStyle.italic),),
),
SizedBox(height: 20.0),
Center(child: Text("$digithours:$digitminutes:$digitsecounds",style: TextStyle(color: Colors.white,fontSize: 73.0,fontWeight: FontWeight.bold),),),
Container(
height: 300.0,
decoration: BoxDecoration(color: Color.fromARGB(255, 162, 109, 145),
borderRadius: BorderRadius.circular(10)),
child: ListView.builder(
itemCount: laps.length,
itemBuilder: (context,index){
return Padding(
padding: const EdgeInsets.all(18.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Lap Number = ${index 1}",style: TextStyle(color: Colors.white,fontSize: 16 ),),
Text("${laps[index]}",style:TextStyle(color: Colors.white,fontSize: 16 ) )
],
),
);
}
),
),
SizedBox(height: 23),
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: RawMaterialButton(onPressed: (){
(!started) ? start():stop();
},
fillColor: Color.fromARGB(255, 73, 119, 4),
shape:StadiumBorder(side: BorderSide(color: Colors.blue)),
child: Text((! started)? "START":"PAUSE",style: TextStyle(color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic
),
),
)
),
SizedBox(width: 8),
IconButton(
onPressed: (){addlaps();}, icon: Icon(Icons.timelapse_rounded,color: Colors.red,)),
Expanded(child: RawMaterialButton(onPressed: (){reset();},
fillColor: Color.fromARGB(255, 132, 9, 23),
shape:StadiumBorder(side: BorderSide(color: Color.fromARGB(255, 40, 129, 11))),
child: Text("RESET",style: TextStyle(color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic
),
),
)
)
],
)
],
),
),
),
);
}
}
please any one can help me to get rid of this timer functionality error
#the digitsecound is not restarting after completing sixty secounds
#Anyone who can help? I'm definitely missing something.
CodePudding user response:
The problem lies in this part:
else{localMinutes ;localSecounds==0;}
You are checking if localSecounds is equal to 0, not actually giving it the value 0(Same thing with minutes). Try:
if (localSecounds>59) {
if (localMinutes>59) {
localHours ;
localMinutes=0;
}else{
localMinutes ;
localSecounds=0;
}
}
CodePudding user response:
Check this start Timer Function, it may help you out
late Timer _timer;
int seconds = 00;
int minutes = 00;
int hours = 00;
void startTimer() {
const oneSec = const Duration(seconds: 1);
_timer = new Timer.periodic(
oneSec,
(Timer timer) => setState(
() {
seconds ;
if (seconds > 59) {
minutes = 1;
seconds = 0;
if (minutes > 59) {
hours = 1;
minutes = 0;
}
}
},
),
);
}
CodePudding user response:
Or User can use this simple but powerful package : stop_watch_timer from pub.dev