import 'dart:async'; import 'package:flutter/material.dart'; class PlayerClockText extends StatefulWidget { const PlayerClockText({super.key, this.style}); final TextStyle? style; @override State createState() => _PlayerClockTextState(); } class _PlayerClockTextState extends State { Timer? _timer; late String _text; @override void initState() { super.initState(); _text = formatClock(DateTime.now()); _scheduleNextTick(); } void _scheduleNextTick() { final now = DateTime.now(); final next = DateTime( now.year, now.month, now.day, now.hour, now.minute, ).add(const Duration(minutes: 1)); _timer = Timer(next.difference(now), _tick); } void _tick() { if (!mounted) return; setState(() => _text = formatClock(DateTime.now())); _scheduleNextTick(); } @override void dispose() { _timer?.cancel(); super.dispose(); } @override Widget build(BuildContext context) { return Text( _text, style: widget.style ?? const TextStyle( color: Colors.white70, fontSize: 12, height: 1.0, letterSpacing: 0.5, fontFeatures: [FontFeature.tabularFigures()], ), ); } } String formatClock(DateTime t) { final h = t.hour.toString().padLeft(2, '0'); final m = t.minute.toString().padLeft(2, '0'); return '$h:$m'; }