import 'dart:math' as math; import 'package:flutter/widgets.dart'; import '../motion/motion.dart'; import 'pop_entrance.dart'; /// The animations of one floating tab bar: its tab buttons popping in one /// after another, and the newly selected icon's bounce. /// /// They live with the bar, not with each button, so a button rebuilt with a /// new selection (or a new badge) never restarts its entrance: only the bar /// decides when its tabs enter. class FloatingTabMotion { FloatingTabMotion(TickerProvider vsync) : entrance = AnimationController(vsync: vsync), bounce = AnimationController( vsync: vsync, duration: Motion.tabBounceDuration, value: 1, ); /// Drives every tab's entrance; tab n plays its pop in its own window. final AnimationController entrance; final AnimationController bounce; int _count = 0; final List _entrances = []; int _selected = 0; /// Plays the entrance of a bar of [count] tabs, from the start, with /// [selected] chosen and nothing bouncing. void enter( BuildContext context, { required int count, required int selected, }) { _count = count; final duration = Motion.of(context, Motion.tabEntranceDuration); entrance.duration = duration + Motion.tabEntranceStagger * math.max(0, count - 1); if (duration == Duration.zero) { entrance.value = 1; } else { entrance.forward(from: 0); } _splitEntrance(); _selected = selected; bounce.value = 1; } /// Bounces the newly [selected] tab's icon; nothing moves under reduced /// motion. void select(BuildContext context, int selected) { if (selected == _selected) return; _selected = selected; if (Motion.of(context, Motion.tabBounceDuration) == Duration.zero) { bounce.value = 1; return; } bounce.forward(from: 0); } /// The scale of tab [index]'s icon: only the newly selected one bounces. double scaleOf(int index) { if (index != _selected || bounce.isCompleted) return 1; final t = bounce.value; final rise = t < 0.5 ? t * 2 : (1 - t) * 2; return 1 + (Motion.tabBounceScale - 1) * Curves.easeOut.transform(rise); } /// Tab [index]'s share of [entrance]: its pop, then its fade. PopAnimations entranceOf(int index) => index >= 0 && index < _entrances.length ? _entrances[index] : poppedIn; /// Splits [entrance] into each tab's window, once per entrance. void _splitEntrance() { _disposeEntrances(); final total = entrance.duration!; if (total == Duration.zero) return; for (var index = 0; index < _count; index += 1) { _entrances.add( popWindow( entrance, start: Motion.tabEntranceStagger * index, total: total, ), ); } } void _disposeEntrances() { _entrances.forEach(disposePopWindow); _entrances.clear(); } void dispose() { _disposeEntrances(); entrance.dispose(); bounce.dispose(); } } /// Tab [index]'s whole button popping in with the bar: the app's shared /// [PopTransition], its glass fading in with its blur. class FloatingTabEntrance extends StatelessWidget { const FloatingTabEntrance({ super.key, required this.motion, required this.index, required this.child, }); final FloatingTabMotion motion; final int index; final Widget child; @override Widget build(BuildContext context) => PopTransition(animations: motion.entranceOf(index), child: child); } /// Tab [index]'s icon, bouncing briefly when it becomes selected. class FloatingTabBounce extends StatelessWidget { const FloatingTabBounce({ super.key, required this.motion, required this.index, required this.child, }); final FloatingTabMotion motion; final int index; final Widget child; @override Widget build(BuildContext context) => AnimatedBuilder( animation: motion.bounce, child: child, builder: (context, child) => Transform.scale(scale: motion.scaleOf(index), child: child), ); }