import 'package:flutter/widgets.dart'; import '../motion/motion.dart'; import 'glass_pill.dart'; /// One item's share of an entrance: its pop (0 -> 1, with an overshoot) and /// its fade (0 -> 1). typedef PopAnimations = ({Animation pop, Animation opacity}); /// An item that has already entered. const PopAnimations poppedIn = ( pop: kAlwaysCompleteAnimation, opacity: kAlwaysCompleteAnimation, ); /// The window of [parent] (running over [total]) in which an item that /// starts popping at [start] pops: [Motion.tabEntranceDuration] long, fading /// in over its first [Motion.tabEntranceOpacityPortion]. /// /// The caller disposes the result with [disposePopWindow]. PopAnimations popWindow( Animation parent, { required Duration start, required Duration total, }) { final whole = total.inMicroseconds; if (whole == 0) return poppedIn; final from = start.inMicroseconds; final window = Motion.tabEntranceDuration.inMicroseconds; final begin = (from / whole).clamp(0.0, 1.0); final end = ((from + window) / whole).clamp(begin, 1.0); final faded = ((from + window * Motion.tabEntranceOpacityPortion) / whole) .clamp(begin, 1.0); return ( pop: CurvedAnimation( parent: parent, curve: Interval(begin, end, curve: Motion.tabEntranceCurve), ), opacity: CurvedAnimation( parent: parent, curve: Interval(begin, faded, curve: Curves.easeOut), ), ); } /// Releases the curves of a [popWindow]. void disposePopWindow(PopAnimations animations) { if (animations.pop case final CurvedAnimation curved) curved.dispose(); if (animations.opacity case final CurvedAnimation curved) curved.dispose(); } /// The app's pop-in, shared by the floating tab buttons and every piece of /// the top bar: [child] grows from [Motion.tabEntranceScaleBegin] with a /// gentle overshoot, rising [Motion.tabEntranceSlideDistance], while it /// fades in. /// /// A [glass] item fades its [GlassPill] through a [GlassReveal], so the blur /// fades with the fill instead of snapping on at the end (an opacity layer /// would hide the backdrop from a grouped blur); anything else fades as a /// whole. Either way it stays announced (and findable) while it fades. class PopTransition extends StatelessWidget { const PopTransition({ super.key, required this.animations, required this.child, this.glass = true, }); final PopAnimations animations; final Widget child; final bool glass; @override Widget build(BuildContext context) => AnimatedBuilder( animation: animations.pop, child: glass ? GlassReveal(opacity: animations.opacity, child: child) : FadeTransition( opacity: animations.opacity, alwaysIncludeSemantics: true, child: child, ), builder: (context, child) { final pop = animations.pop.value; return Transform.translate( offset: Offset(0, (1 - pop) * Motion.tabEntranceSlideDistance), child: Transform.scale( scale: Motion.tabEntranceScaleBegin + (1 - Motion.tabEntranceScaleBegin) * pop, child: child, ), ); }, ); } /// A [PopTransition] that drives itself: [child] pops in once, after /// [delay], when it is first built. /// /// Rebuilding it (a new title, new neighbours, a scroll) never plays it /// again, so a host keys its items to keep them while they stay on screen. /// Under reduced motion, or when [play] is false, it simply appears. class PopEntrance extends StatefulWidget { const PopEntrance({ super.key, required this.child, this.delay = Duration.zero, this.play = true, this.glass = true, }); final Widget child; final Duration delay; final bool play; final bool glass; @override State createState() => _PopEntranceState(); } class _PopEntranceState extends State with SingleTickerProviderStateMixin { AnimationController? _controller; PopAnimations _animations = poppedIn; bool _decided = false; @override void didChangeDependencies() { super.didChangeDependencies(); // Decided once, on the first build: later dependency changes (a theme, // a text scale) never replay it. if (_decided) return; _decided = true; final window = Motion.of(context, Motion.tabEntranceDuration); if (!widget.play || window == Duration.zero) return; final total = widget.delay + window; final controller = AnimationController(vsync: this, duration: total); _controller = controller; _animations = popWindow(controller, start: widget.delay, total: total); controller.forward(); } @override void dispose() { disposePopWindow(_animations); _controller?.dispose(); super.dispose(); } @override Widget build(BuildContext context) => PopTransition( animations: _animations, glass: widget.glass, child: widget.child, ); }