Clean up the formatting a little and add duration to lap
This commit is contained in:
@@ -84,19 +84,39 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
if (_isRunning) {
|
if (_isRunning) {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
setState(() {
|
setState(() {
|
||||||
_laps.add(now.toIso8601String());
|
// Store both timestamp and formatted duration for this lap
|
||||||
|
_laps.add('${now.toIso8601String()} - ${_formatDuration(_elapsedTime)}');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String _formatDuration(Duration duration) {
|
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 hours = duration.inHours;
|
||||||
final minutes = duration.inMinutes.remainder(60);
|
final minutes = duration.inMinutes.remainder(60);
|
||||||
final seconds = duration.inSeconds.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) {
|
void _handleKeyEvent(RawKeyEvent event) {
|
||||||
@@ -152,9 +172,15 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
style: Theme.of(context).textTheme.headlineSmall,
|
style: Theme.of(context).textTheme.headlineSmall,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
Text(
|
Container(
|
||||||
_formatDuration(_elapsedTime),
|
width: 300, // Fixed width container to prevent "flashing"
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
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),
|
const SizedBox(height: 30),
|
||||||
Row(
|
Row(
|
||||||
|
Reference in New Issue
Block a user