我有一个
StopWatchWidget
在我的应用程序中,我用它来创建自己的计时器。它向上计数,包括分钟、秒和毫秒。问题是毫秒占用3位数字,而我希望它们只占用2位数字。我怎么能省略最后一个数字?如果有助于传达我的观点,我附上了一张图片:
这是我的代码:
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')}';