WIP: fade-through tab transitions (run #228)
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
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<Widget> children;
|
||||
|
||||
@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;
|
||||
|
||||
@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 = reduced || active || outgoing;
|
||||
final progress = _controller.value;
|
||||
final opacity = outgoing
|
||||
? 1 -
|
||||
CurvedAnimation(
|
||||
parent: _controller,
|
||||
curve: const Interval(
|
||||
0,
|
||||
Motion.tabFadeThroughIncomingAt,
|
||||
curve: Motion.curve,
|
||||
),
|
||||
).value
|
||||
: active
|
||||
? CurvedAnimation(
|
||||
parent: _controller,
|
||||
curve: const Interval(
|
||||
Motion.tabFadeThroughIncomingAt,
|
||||
1,
|
||||
curve: Motion.curve,
|
||||
),
|
||||
).value
|
||||
: 0.0;
|
||||
final scale = outgoing
|
||||
? 1 -
|
||||
(Motion.tabFadeThroughScale - 1).abs() *
|
||||
(progress / Motion.tabFadeThroughIncomingAt).clamp(0.0, 1.0)
|
||||
: active
|
||||
? Motion.tabFadeThroughScale +
|
||||
(1 - Motion.tabFadeThroughScale) *
|
||||
((progress - Motion.tabFadeThroughIncomingAt) /
|
||||
(1 - Motion.tabFadeThroughIncomingAt))
|
||||
.clamp(0.0, 1.0)
|
||||
: 1.0;
|
||||
final transformed = Transform.scale(
|
||||
scale: scale,
|
||||
child: Opacity(opacity: opacity, child: widget.children[index]),
|
||||
);
|
||||
final page = active
|
||||
? Semantics(container: true, explicitChildNodes: true, child: transformed)
|
||||
: ExcludeFocus(
|
||||
child: ExcludeSemantics(child: transformed),
|
||||
);
|
||||
return Offstage(
|
||||
offstage: !shown,
|
||||
child: TickerMode(
|
||||
enabled: active,
|
||||
child: IgnorePointer(
|
||||
ignoring: !active,
|
||||
child: page,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user