skip to Main Content

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


  1. Chosen as BEST ANSWER

    Ok, I figured out a solution. I can get the current time in a Stream and use that information to handle the time adjustment.

      void subtractTime() async {
        Future<int> result = _stopWatchTimer.minuteTime.first;
        int currentTime = await result;
        if (currentTime < 5) {
          _stopWatchTimer.clearPresetTime();
          _stopWatchTimer.onResetTimer();
        } else {
          _stopWatchTimer.setPresetMinuteTime(-5);
        }
      }
    

  2. 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?

    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: () {
              if (_stopWatchTimer.getPresetMinuteTime() < 5) {
                _stopWatchTimer.setPresetMinuteTime(0);
              } else {
                _stopWatchTimer.setPresetMinuteTime(
                  _stopWatchTimer.getPresetMinuteTime() - 5,
                );
              }
            },
            child: const Text('+ 5 Minutes'),
          ),
        ),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 5),
          child: ElevatedButton(
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.amber,
            ),
            onPressed: () {
              if (_stopWatchTimer.getPresetMinuteTime() < 5) {
                _stopWatchTimer.setPresetMinuteTime(0);
              } else {
                _stopWatchTimer.setPresetMinuteTime(
                  _stopWatchTimer.getPresetMinuteTime() + 5,
                );
              }
            },
            child: const Text('-5 Minutes'),
          ),
        ),
      ],
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search