Merge commit '72128d53685269ff1e13d2c76c80b6276f7635ba' into sticky-section-clip
This commit is contained in:
@@ -39,6 +39,7 @@ class AppListRow extends StatelessWidget {
|
||||
required this.title,
|
||||
this.leading,
|
||||
this.time,
|
||||
this.timeWidget,
|
||||
this.subtitle,
|
||||
this.compactSubtitle,
|
||||
this.subtitleLeading,
|
||||
@@ -59,6 +60,7 @@ class AppListRow extends StatelessWidget {
|
||||
final Widget title;
|
||||
final Widget? leading;
|
||||
final String? time;
|
||||
final Widget? timeWidget;
|
||||
final Widget? subtitle;
|
||||
|
||||
/// Replaces [subtitle] in a compact row, which has less room for facts.
|
||||
@@ -204,17 +206,25 @@ class AppListRow extends StatelessWidget {
|
||||
child: title,
|
||||
),
|
||||
),
|
||||
if (!compact && time != null && time!.isNotEmpty)
|
||||
if (!compact &&
|
||||
(timeWidget != null || (time != null && time!.isNotEmpty)))
|
||||
ConstrainedBox(
|
||||
constraints: BoxConstraints(maxWidth: constraints.maxWidth * 0.4),
|
||||
child: Padding(
|
||||
padding: const EdgeInsetsDirectional.only(start: 8),
|
||||
child: Text(
|
||||
time!,
|
||||
style: styles.time,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
child: timeWidget == null
|
||||
? Text(
|
||||
time!,
|
||||
style: styles.time,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
)
|
||||
: DefaultTextStyle(
|
||||
style: styles.time,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
child: timeWidget!,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'relative_time.dart';
|
||||
|
||||
/// A relative-time label that rebuilds only itself when its formatter can
|
||||
/// change. It stops its timer while the app is backgrounded.
|
||||
class RelativeTimeLabel extends StatefulWidget {
|
||||
const RelativeTimeLabel({super.key, required this.instant, this.now});
|
||||
|
||||
final DateTime? instant;
|
||||
final DateTime? now;
|
||||
|
||||
@override
|
||||
State<RelativeTimeLabel> createState() => _RelativeTimeLabelState();
|
||||
}
|
||||
|
||||
class _RelativeTimeLabelState extends State<RelativeTimeLabel>
|
||||
with WidgetsBindingObserver {
|
||||
Timer? _timer;
|
||||
DateTime _now = DateTime.now();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_schedule();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(RelativeTimeLabel oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.instant != widget.instant) _schedule();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
_now = DateTime.now();
|
||||
if (mounted) setState(() {});
|
||||
_schedule(current: _now);
|
||||
} else if (state == AppLifecycleState.hidden ||
|
||||
// Not inactive: a desktop window that lost focus is still visible.
|
||||
state == AppLifecycleState.paused ||
|
||||
state == AppLifecycleState.detached) {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
}
|
||||
}
|
||||
|
||||
void _schedule({DateTime? current}) {
|
||||
_timer?.cancel();
|
||||
final instant = widget.instant;
|
||||
if (instant == null) return;
|
||||
final now = current ?? widget.now ?? DateTime.now();
|
||||
_now = now;
|
||||
final next = _nextBoundary(instant, now);
|
||||
final delay = next.difference(now);
|
||||
_timer = Timer(delay.isNegative ? Duration.zero : delay, () {
|
||||
if (!mounted) return;
|
||||
_now = _now.add(delay);
|
||||
setState(() {});
|
||||
_schedule(current: _now);
|
||||
});
|
||||
}
|
||||
|
||||
DateTime _nextBoundary(DateTime instant, DateTime now) {
|
||||
final difference = now.difference(instant);
|
||||
final age = difference.abs();
|
||||
final unit = age < const Duration(hours: 1)
|
||||
? const Duration(minutes: 1)
|
||||
: age < const Duration(days: 1)
|
||||
? const Duration(hours: 1)
|
||||
: age < const Duration(days: 7)
|
||||
? const Duration(days: 1)
|
||||
: null;
|
||||
if (unit != null) {
|
||||
final units = age.inMilliseconds ~/ unit.inMilliseconds;
|
||||
final boundary = difference.isNegative
|
||||
// Future labels use a decreasing count. Add a second so an exact
|
||||
// boundary does not immediately schedule another zero-delay timer.
|
||||
? instant.subtract(unit * units).add(const Duration(seconds: 1))
|
||||
: instant.add(unit * (units + 1));
|
||||
if (boundary.isAfter(now)) return boundary;
|
||||
}
|
||||
final midnight = DateTime(now.year, now.month, now.day + 1);
|
||||
return midnight.isAfter(now)
|
||||
? midnight
|
||||
: now.add(const Duration(minutes: 1));
|
||||
}
|
||||
|
||||
@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() {
|
||||
_timer?.cancel();
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user