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. class FadeThroughStack extends StatefulWidget { const FadeThroughStack({ super.key, required this.index, required this.children, }); final int index; final List children; @override State createState() => _FadeThroughStackState(); } class _FadeThroughStackState extends State with SingleTickerProviderStateMixin { late final AnimationController _controller = AnimationController( vsync: this, duration: Motion.tabFadeThrough, ); late int _visibleIndex = widget.index; int? _outgoingIndex; @override void initState() { super.initState(); _controller.value = 1; _controller.addStatusListener(_statusChanged); } @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; 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.children[index]), ), ), ), ), ), ); } }