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
),
),
),
],
),
),
),
],
),
);
}
CodePudding user response:
Dart has a Stopwatch
class. For each lap, you can simply record its current elapsed
value. Example usage:
void main() async {
var stopwatch = Stopwatch()..start();
var lapTimes = <Duration>[];
for (var i = 0; i < 5; i = 1) {
var randomSeconds = random.nextInt(5) 1;
print(randomSeconds);
await Future.delayed(Duration(seconds: randomSeconds));
lapTimes.add(stopwatch.elapsed);
}
print(lapTimes);
}
Note that the above would record the cumulative duration for the laps (i.e., the Durations
will be monotonically increasing); if you instead want each individual lap's duration, then you would have to subtract each Duration
from its previous one:
var lapDeltas = [
lapTimes[0],
for (var i = 1; i < lapTimes.length; i = 1)
lapTimes[i] - lapTimes[i - 1],
];
print(lapDeltas);
Alternatively you could easily implement Stopwatch
yourself with DateTime.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).
CodePudding user response:
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.
int lapCounter = 0;
Then, in your timer counter function, where you end the timer
....myTimer ends here before restarting the counter.
setState(()=> lapCounter = 1);
And then function where you reset the progress to zero.
setState(()=> lapCounter = 0);