I am looking for a way to record times while having a constant timer running, almost like a lap function. I have the stopwatch setup, just need a way to create a lap function. I need it be able to record a lap while still running till it is stopped or another lap is initiated. Can anyone help? Will provide code if needed. Thank you!
*edit added the code of my stopwatch
Widget stopwatch(){
return Container(
child: Column(
children: <Widget>[
Expanded(
flex: 6,
child: Container(
alignment: Alignment.center,
child: Text(
stoptimedisplay,
style: TextStyle(
fontSize: 50.0,
fontWeight: FontWeight.bold,
),
),
),
),
Expanded(
flex: 4,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
onPressed: stoppressed ? null : stopstopwatch,
color: Colors.red,
padding: EdgeInsets.symmetric(
horizontal: 40.0,
vertical: 12.0,
),
child: Text(
"Stop",
style: TextStyle(
fontSize: 20.0,
color: Colors.white
),
),
),
RaisedButton(
onPressed: resetpressed ? null : resetstopwatch,
color: Colors.blueGrey,
padding: EdgeInsets.symmetric(
horizontal: 40.0,
vertical: 12.0,
),
child: Text(
"Reset",
style: TextStyle(
fontSize: 20.0,
color: Colors.white
),
),
),
],
),
RaisedButton(
onPressed: startpressed ? startstopwatch : null,
color: Colors.green,
padding: EdgeInsets.symmetric(
horizontal: 80.0,
vertical: 20.0,
),
child: Text(
"Start",
style: TextStyle(
fontSize: 24.0,
color: Colors.white
),
),
),
],
),
),
),
],
),
);
}
2
Answers
Please post your code for details, but as far as I understood, I think you can create another lapCounter variable initialized as 0 at first.
Then, in your timer counter function, where you end the timer
And then function where you reset the progress to zero.
Dart has a
Stopwatch
class. For each lap, you can simply record its currentelapsed
value. Example usage:Note that the above would record the cumulative duration for the laps (i.e., the
Duration
s will be monotonically increasing); if you instead want each individual lap’s duration, then you would have to subtract eachDuration
from its previous one:Alternatively you could easily implement
Stopwatch
yourself withDateTime.now()
by recording a starting timestamp, recording timestamps for each lap, and then computing the differences between the current timestamp and the starting timestamp (or between the current timestamp and the previous timestamp if you want non-cumulative durations).