Files
sm-emby-share/lib/features/player/widgets/player_clock_text.dart
T
2026-07-14 11:11:36 +08:00

73 lines
1.4 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
class PlayerClockText extends StatefulWidget {
const PlayerClockText({super.key, this.style});
final TextStyle? style;
@override
State<PlayerClockText> createState() => _PlayerClockTextState();
}
class _PlayerClockTextState extends State<PlayerClockText> {
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';
}