Clean up the formatting a little and add duration to lap

This commit is contained in:
2025-03-14 23:34:10 +01:00
parent 969b146b35
commit b7a7eee777

View File

@@ -84,19 +84,39 @@ class _MyHomePageState extends State<MyHomePage> {
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<MyHomePage> {
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 10),
Text(
Container(
width: 300, // Fixed width container to prevent "flashing"
alignment: Alignment.center,
child: Text(
_formatDuration(_elapsedTime),
style: Theme.of(context).textTheme.headlineMedium,
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
fontFamily: 'monospace', // Use monospaced font for fixed-width characters
),
),
),
const SizedBox(height: 30),
Row(