3b996bdbc7
Relative-time labels follow one wall-minute RelativeTimeClock instead of a timer per row, skip ticks while their tab is offstage and rebuild only when their text changes. The shell rebuilds its top bar only for the focused destination's published bar and its tab bar only for badge changes. Run events that repeat a row's state no longer notify, and the runs list filters and sorts once per build. Adds a rebuild/timer benchmark.
95 lines
2.5 KiB
Dart
95 lines
2.5 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
import 'relative_time.dart';
|
|
import 'relative_time_clock.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 = TickerMode.valuesOf(context).enabled;
|
|
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();
|
|
}
|
|
}
|