I’m trying to incorporate a stopwatch into my app and I’m using the stop_watch_timer package (https://pub.dev/packages/stop_watch_timer). My issue lies with 2 buttons I’m adding; one to add 5 minutes to the current display time, and another to subtract 5 minutes from the current display time.
I have a hacked together approach where I modify the preset time, which in turn adjusts the current display time. Then, when I reset the display time I first call clearPresetTime() then onResetTimer() which works alright.
My problem, is that I am using setPresetMinuteTime(-5) to subtract 5 minutes from the display time, and this causes an integer underflow error where the time will go from 0:00:00 to -1:55:00.
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 5),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey),
onPressed: () => [
_stopWatchTimer.clearPresetTime(),
_stopWatchTimer.onResetTimer()
],
child: const Text('Reset')),
)
],
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 5),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.lightBlue),
onPressed: () => _stopWatchTimer.setPresetMinuteTime(5),
child: const Text('+ 5 Minutes'),
)),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 5),
child: ElevatedButton(
style:
ElevatedButton.styleFrom(backgroundColor: Colors.amber),
onPressed: () => _stopWatchTimer.setPresetMinuteTime(-5),
child: const Text('-5 Minutes')),
)
])
2
Answers
Ok, I figured out a solution. I can get the current time in a Stream and use that information to handle the time adjustment.
I’ve made a few edits to your code. But unfortunately, I don’t have a test environment for it to be working. Do you have a chance to try what I wrote below, please?