import 'dart:math' as math; import 'package:flutter/widgets.dart'; import '../motion/motion.dart'; import 'glass_pill.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<({Animation pop, Animation opacity})> _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. ({Animation pop, Animation opacity}) entranceOf(int index) => index >= 0 && index < _entrances.length ? _entrances[index] : (pop: kAlwaysCompleteAnimation, opacity: kAlwaysCompleteAnimation); /// Splits [entrance] into each tab's window, once per entrance. void _splitEntrance() { _disposeEntrances(); final total = entrance.duration!.inMicroseconds; if (total == 0) return; final window = Motion.tabEntranceDuration.inMicroseconds; for (var index = 0; index < _count; index += 1) { final start = Motion.tabEntranceStagger.inMicroseconds * index; final begin = (start / total).clamp(0.0, 1.0); final end = ((start + window) / total).clamp(begin, 1.0); final faded = ((start + window * Motion.tabEntranceOpacityPortion) / total).clamp( begin, 1.0, ); _entrances.add(( pop: CurvedAnimation( parent: entrance, curve: Interval(begin, end, curve: Curves.easeOutBack), ), opacity: CurvedAnimation( parent: entrance, curve: Interval(begin, faded, curve: Curves.easeOut), ), )); } } void _disposeEntrances() { for (final animations in _entrances) { (animations.pop as CurvedAnimation).dispose(); (animations.opacity as CurvedAnimation).dispose(); } _entrances.clear(); } void dispose() { _disposeEntrances(); entrance.dispose(); bounce.dispose(); } } /// Tab [index]'s whole button popping in with the bar: it grows from /// [Motion.tabEntranceScaleBegin] with a gentle overshoot, rising a little, /// while its glass fades in through a [GlassReveal] (so the blur fades with /// the fill instead of snapping on at the end). 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) { final entrance = motion.entranceOf(index); return AnimatedBuilder( animation: entrance.pop, // The reveal keeps the tab (and its badge count) announced while it // fades in. child: GlassReveal(opacity: entrance.opacity, child: child), builder: (context, child) { final pop = entrance.pop.value; final scale = Motion.tabEntranceScaleBegin + (1 - Motion.tabEntranceScaleBegin) * pop; return Transform.translate( offset: Offset(0, (1 - pop) * Motion.tabEntranceSlideDistance), child: Transform.scale(scale: scale, 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), ); }