duration static method

Duration? duration(
  1. String value
)

A duration from 250ms, 250 ms, 4s, 1.5s, 2m, 1h, 0.5d (units ms, s, m, h, d, in any case; decimals allowed), or plain milliseconds; null otherwise. The one duration syntax of the theme: motion, animations, styles, tokens, transition, refresh and timeout.

Implementation

static Duration? duration(String value) {
  final text = value.trim();
  final match = _durationPattern.firstMatch(text);
  if (match == null) {
    final plain = RegExp(r'^(\d+(?:\.\d+)?|\.\d+)$').hasMatch(text);
    final ms = plain ? double.tryParse(text) : null;
    return ms == null ? null : Duration(microseconds: (ms * 1000).round());
  }
  final amount = double.parse(match[1]!);
  final unit = match[2]!.toLowerCase();
  final ms = switch (unit) {
    's' => amount * 1000,
    'm' => amount * 60000,
    'h' => amount * 3600000,
    'd' => amount * 86400000,
    _ => amount,
  };
  return Duration(microseconds: (ms * 1000).round());
}