I have a StopWatchWidget
in my app, and I have used it to create my own timer. It counts upwards, including minutes, seconds, and milliseconds. The issue is that the milliseconds take up 3 digits, whilst I want them to only take up 2 digits. How can I omit the last digit? I have attached an image, if it helps to get my point across:
Here is my code:
class StopWatchWidgetState extends State<StopWatchWidget> {
late Color timercolor = Colors.white;
final Stopwatch _stopwatch = Stopwatch();
String stopWatchText = '0.00.00';
void startStopWatch() {
if (_stopwatch.isRunning) {
stopWatchText = "0.00.00";
_stopwatch.stop();
} else {
stopWatchText = '0.00.00';
_stopwatch
..reset()
..start();
}
Timer.periodic(const Duration(milliseconds: 1), (timer) {
setState(() {
if (!_stopwatch.isRunning) {
timer.cancel();
}
stopWatchText =
'${(_stopwatch.elapsed.inMinutes % 60).toString().padLeft(2, '')}.${(_stopwatch.elapsed.inSeconds % 60).toString().padLeft(2, '0')}.${(_stopwatch.elapsed.inMilliseconds % 1000).toString().padLeft(2, '0')}';
Have you tried to divide the milliseconds by 10 so it shows only 2 digits
stopWatchText =
'${(_stopwatch.elapsed.inMinutes % 60).toString().padLeft(2, '')}.${(_stopwatch.elapsed.inSeconds % 60).toString().padLeft(2, '0')}.${((_stopwatch.elapsed.inMilliseconds % 1000) ~/ 10).toString().padLeft(2, '0')}';