diff --git a/lib/main.dart b/lib/main.dart index 805c860..0e9b823 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -84,19 +84,39 @@ class _MyHomePageState extends State { if (_isRunning) { final now = DateTime.now(); setState(() { - _laps.add(now.toIso8601String()); + // Store both timestamp and formatted duration for this lap + _laps.add('${now.toIso8601String()} - ${_formatDuration(_elapsedTime)}'); }); } } String _formatDuration(Duration duration) { - // Format the duration as ISO 8601 duration format: PT[hours]H[minutes]M[seconds]S + // Format the duration as ISO 8601 duration format without 0 values final hours = duration.inHours; final minutes = duration.inMinutes.remainder(60); final seconds = duration.inSeconds.remainder(60); - final milliseconds = duration.inMilliseconds.remainder(1000); - return 'PT${hours}H${minutes}M${seconds}.${milliseconds}S'; + // Round milliseconds to nearest 100ms for more consistent display + // This helps avoid floating point inconsistencies (0.201, 0.301, etc.) + final milliseconds = ((duration.inMilliseconds % 1000) / 100).round() * 100; + final formattedMs = (milliseconds ~/ 100).toString(); + + // Start with PT prefix + String result = 'PT'; + + // Only add parts that are non-zero (except if everything is zero, then show 0S) + if (hours > 0) { + result += '${hours}H'; + } + + if (minutes > 0 || hours > 0) { + result += '${minutes}M'; + } + + // Always include seconds with a single decimal place (tenth of a second) + result += '${seconds}.${formattedMs}S'; + + return result; } void _handleKeyEvent(RawKeyEvent event) { @@ -152,9 +172,15 @@ class _MyHomePageState extends State { style: Theme.of(context).textTheme.headlineSmall, ), const SizedBox(height: 10), - Text( - _formatDuration(_elapsedTime), - style: Theme.of(context).textTheme.headlineMedium, + Container( + width: 300, // Fixed width container to prevent "flashing" + alignment: Alignment.center, + child: Text( + _formatDuration(_elapsedTime), + style: Theme.of(context).textTheme.headlineMedium?.copyWith( + fontFamily: 'monospace', // Use monospaced font for fixed-width characters + ), + ), ), const SizedBox(height: 30), Row(