7ff012880b
Provide composable shown/current visibility for retained surfaces and migrate visibility readers away from TickerMode. This keeps ticker muting separate from painted and current-surface state.
96 lines
2.6 KiB
Dart
96 lines
2.6 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
import 'relative_time.dart';
|
|
import 'relative_time_clock.dart';
|
|
import '../motion/surface_visibility.dart';
|
|
|
|
/// A relative-time label that rebuilds only itself, and only when its text
|
|
/// changes. Every label follows one shared minute [RelativeTimeClock] rather
|
|
/// than a timer of its own; a label whose tickers are muted (an offstage tab
|
|
/// or a covered route) skips the ticks and catches up when shown again.
|
|
class RelativeTimeLabel extends StatefulWidget {
|
|
const RelativeTimeLabel({
|
|
super.key,
|
|
required this.instant,
|
|
this.now,
|
|
this.clock,
|
|
});
|
|
|
|
final DateTime? instant;
|
|
|
|
/// The time the label starts from; the shared clock advances it.
|
|
final DateTime? now;
|
|
|
|
/// The clock to follow; [RelativeTimeClock.shared] when null.
|
|
final RelativeTimeClock? clock;
|
|
|
|
@override
|
|
State<RelativeTimeLabel> createState() => _RelativeTimeLabelState();
|
|
}
|
|
|
|
class _RelativeTimeLabelState extends State<RelativeTimeLabel> {
|
|
late RelativeTimeClock _clock;
|
|
late DateTime _now;
|
|
bool _shown = true;
|
|
bool _stale = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_now = widget.now ?? DateTime.now();
|
|
_clock = widget.clock ?? RelativeTimeClock.shared;
|
|
_clock.addListener(_tick);
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(RelativeTimeLabel oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
final clock = widget.clock ?? RelativeTimeClock.shared;
|
|
if (clock == _clock) return;
|
|
_clock.removeListener(_tick);
|
|
_clock = clock..addListener(_tick);
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
_shown = SurfaceVisibility.shownOf(context);
|
|
if (_shown && _stale) _advance();
|
|
}
|
|
|
|
/// Moves to the clock's time; true when the text changed.
|
|
bool _advance() {
|
|
_stale = false;
|
|
final next = _clock.now;
|
|
if (!next.isAfter(_now)) return false;
|
|
final before = formatRelativeTime(widget.instant, now: _now);
|
|
_now = next;
|
|
return formatRelativeTime(widget.instant, now: _now) != before;
|
|
}
|
|
|
|
void _tick() {
|
|
if (!mounted || widget.instant == null) return;
|
|
if (!_shown) {
|
|
_stale = true;
|
|
return;
|
|
}
|
|
if (_advance()) setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// The row's time style (an unread row's accent) stays on the label
|
|
// itself, as on the plain Text this replaces.
|
|
return Text(
|
|
formatRelativeTime(widget.instant, now: _now) ?? '',
|
|
style: DefaultTextStyle.of(context).style,
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_clock.removeListener(_tick);
|
|
super.dispose();
|
|
}
|
|
}
|