f1ac647125
FadeThroughStack(lazy: true) builds only the shown page first and the rest one per frame once no animation runs. Opening a run detail no longer lays out Timeline, Artifacts and Changes offstage during the route transition (transition build 62-103 ms -> 38-59 ms); first tab switches are unchanged. Adds tool/perf/interaction_bench.mjs (FrameTiming per interaction over the full-app fixture, CPU profiles mapped to Dart files), fixture options for hits/chats/graph, and notes/perf-round3.md with the survey.
180 lines
5.5 KiB
Dart
180 lines
5.5 KiB
Dart
import 'package:flutter/scheduler.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'motion.dart';
|
|
|
|
/// Keeps every child mounted while fading between two indexed pages.
|
|
///
|
|
/// Unlike [AnimatedSwitcher], this never removes a page after it has been
|
|
/// visited, so scroll positions, controllers and local state remain owned by
|
|
/// their original element tree.
|
|
///
|
|
/// With [lazy], the stack first builds only the page on screen. It builds
|
|
/// the others one per frame once no animation is running, or at once when one
|
|
/// is shown first. Opening a page with several tabs then builds one tab during
|
|
/// its route transition, and the first switch to another tab still usually
|
|
/// finds it built.
|
|
class FadeThroughStack extends StatefulWidget {
|
|
const FadeThroughStack({
|
|
super.key,
|
|
required this.index,
|
|
required this.children,
|
|
this.lazy = false,
|
|
});
|
|
|
|
final int index;
|
|
final List<Widget> children;
|
|
final bool lazy;
|
|
|
|
@override
|
|
State<FadeThroughStack> createState() => _FadeThroughStackState();
|
|
}
|
|
|
|
class _FadeThroughStackState extends State<FadeThroughStack>
|
|
with SingleTickerProviderStateMixin {
|
|
late final AnimationController _controller = AnimationController(
|
|
vsync: this,
|
|
duration: Motion.tabFadeThrough,
|
|
);
|
|
late int _visibleIndex = widget.index;
|
|
int? _outgoingIndex;
|
|
|
|
/// The pages built so far; with [FadeThroughStack.lazy], the rest are not
|
|
/// built yet.
|
|
late final Set<int> _visited = {widget.index};
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_controller.value = 1;
|
|
_controller.addStatusListener(_statusChanged);
|
|
if (widget.lazy) _buildNextWhenIdle();
|
|
}
|
|
|
|
/// After each frame: once no animation is running (no transient frame
|
|
/// callbacks, such as a route transition's), builds the first page not
|
|
/// built yet, one per frame, until every page is built.
|
|
void _buildNextWhenIdle() {
|
|
SchedulerBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
final count = widget.children.length;
|
|
final next = [
|
|
for (var i = 0; i < count; i++)
|
|
if (!_visited.contains(i)) i,
|
|
];
|
|
if (next.isEmpty) return;
|
|
if (SchedulerBinding.instance.transientCallbackCount == 0) {
|
|
setState(() => _visited.add(next.first));
|
|
}
|
|
_buildNextWhenIdle();
|
|
});
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant FadeThroughStack oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (widget.index == oldWidget.index) return;
|
|
// A focused control must not remain focused after its page becomes
|
|
// inactive; otherwise keyboard traversal can still reach the old tab.
|
|
FocusManager.instance.primaryFocus?.unfocus(
|
|
disposition: UnfocusDisposition.scope,
|
|
);
|
|
final duration = Motion.of(context, Motion.tabFadeThrough);
|
|
_controller.duration = duration;
|
|
if (duration == Duration.zero) {
|
|
_outgoingIndex = null;
|
|
_visibleIndex = widget.index;
|
|
_controller.value = 1;
|
|
return;
|
|
}
|
|
_outgoingIndex = _visibleIndex;
|
|
_visibleIndex = widget.index;
|
|
_controller.forward(from: 0);
|
|
}
|
|
|
|
void _statusChanged(AnimationStatus status) {
|
|
if (status != AnimationStatus.completed || !mounted) return;
|
|
setState(() => _outgoingIndex = null);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.removeStatusListener(_statusChanged);
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final reduced = Motion.of(context, Motion.tabFadeThrough) == Duration.zero;
|
|
_visited.add(widget.index);
|
|
if (reduced) {
|
|
_outgoingIndex = null;
|
|
_visibleIndex = widget.index;
|
|
_controller.value = 1;
|
|
}
|
|
return AnimatedBuilder(
|
|
animation: _controller,
|
|
builder: (context, _) => Stack(
|
|
fit: StackFit.expand,
|
|
children: [
|
|
for (var index = 0; index < widget.children.length; index++)
|
|
_page(index, reduced),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _page(int index, bool reduced) {
|
|
final outgoing = index == _outgoingIndex;
|
|
final active = index == _visibleIndex;
|
|
final shown = active || outgoing;
|
|
final progress = _controller.value;
|
|
const split = Motion.tabFadeThroughIncomingAt;
|
|
// Curves evaluated directly: a CurvedAnimation built per frame would
|
|
// register a listener on the controller every frame.
|
|
final out = const Interval(
|
|
0,
|
|
split,
|
|
curve: Motion.curve,
|
|
).transform(progress);
|
|
final into = const Interval(
|
|
split,
|
|
1,
|
|
curve: Motion.curve,
|
|
).transform(progress);
|
|
const shrink = 1 - Motion.tabFadeThroughScale;
|
|
final opacity = outgoing ? 1 - out : (active ? into : 0.0);
|
|
final scale = outgoing
|
|
? 1 - shrink * out
|
|
: (active ? Motion.tabFadeThroughScale + shrink * into : 1.0);
|
|
// The same widget structure for every page whatever its state, so a
|
|
// page's element (and its scroll position, controllers and text) is kept
|
|
// across switches.
|
|
return Offstage(
|
|
offstage: !shown,
|
|
child: TickerMode(
|
|
enabled: active,
|
|
child: IgnorePointer(
|
|
ignoring: !active,
|
|
child: ExcludeFocus(
|
|
excluding: !active,
|
|
child: ExcludeSemantics(
|
|
excluding: !active,
|
|
child: Transform.scale(
|
|
scale: scale,
|
|
child: Opacity(
|
|
opacity: opacity,
|
|
child: !widget.lazy || _visited.contains(index)
|
|
? widget.children[index]
|
|
: const SizedBox.shrink(),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|