Home > Back-end >  Flutter | Minutes and seconds on slider
Flutter | Minutes and seconds on slider

Time:08-04

I'm currently coding a flutter app, a timer, and a slider is needed to choose the timer's value.

          children: [
            CupertinoSlider(
                value: _pomodoroValue,
                min: 0.30,
                max: 60,
                divisions: 200,
                onChanged: (selectedValue) {
                  setState(() {
                    _pomodoroValue = selectedValue;
                  });
                }),
            Text('${_pomodoroValue.toString()}')
          ],
        ),

My idea is to have a minimum of 30 seconds to a maximum of 60 minutes, but with the above code, the slider returns just decimals.

How can I have like at the beginning 0:30, then 1:00, 1:30, 2:00 etc...?

Thanks a lot in advance!

CodePudding user response:

Keep in mind the times just is a number, you don't want decimals? use int instead. seconds divider to 60 to get minutes.

void main() {
  getTime(times) {
    final minutes = times ~/ 60;
    final seconds = times % 60;
    formatZero(num) => '$num'.length == 2 ? num : '0$num';
    return "${formatZero(minutes)}:${formatZero(seconds)}";
  }

  print(getTime(30));
  print(getTime(60));
  print(getTime(90));
  print(getTime(99));
}

Result

00:30
01:00
01:30
01:39

CodePudding user response:

Use seconds to keep things simple.

Column(
  children: [
    CupertinoSlider(
      value: _pomodoroValue,
      min: 30,
      max: 3600,
      divisions: (3600 / 30 - 1).toInt(),
      onChanged: (selectedValue) {
        setState(() {
          _pomodoroValue = selectedValue;
        });
      },
    ),
    Text('${Duration(seconds: _pomodoroValue.toInt())}')
  ]
),
  • Related