import 'dart:math' as math; import 'package:flutter/animation.dart'; import 'chat_stack_disc.dart'; import 'chat_stack_pose.dart'; /// How long the stack takes to fan out into its column and to fold back. const Duration chatStackOpenDuration = Duration(milliseconds: 280); const Duration chatStackCloseDuration = Duration(milliseconds: 220); /// Under reduced motion the column only fades in and out, in place. const Duration chatStackFadeDuration = Duration(milliseconds: 120); /// A short spring-like ease: a touch of overshoot on arrival, no bounce back. const Curve chatStackRise = Cubic(.22, 1.18, .36, 1); /// Folding plays the controller backwards: an ease-in in forward time leaves /// the column briskly and settles gently into the pile. const Curve chatStackFold = Curves.easeInCubic; /// Every animation the open column reads, derived once from one controller /// when the items change (never per frame). Item 0 is nearest the thumb, a /// pitch above the stack's slot (where the close button is); the column /// rises from there, each item starting a little after the one below it, /// and folds back top first. class ChatStackMotion { ChatStackMotion({ required Animation parent, required List depths, required this.reduced, required bool rtl, }) : count = depths.length { scrim = _curve(parent, const Interval(0, .7, curve: Curves.easeOut)); close = reduced ? parent : _curve(parent, const Interval(0, .6)); final span = count <= 1 ? 1.0 : .62; final step = count <= 1 ? 0.0 : (1 - span) / (count - 1); for (var i = 0; i < count; i++) { final start = i * step; final end = math.min(1.0, start + span); final depth = depths[i]; // The stack's own slot holds the close button; the column starts a // pitch above it. final to = chatStackColumnPose(i + 1, kChatStackPitch); final layer = chatStackLayers[depth ?? chatStackLayers.length - 1]; target.add(to); if (reduced) { from.add(to); move.add(parent); fade.add(parent); label.add(parent); labelFrom.add(to); continue; } from.add(rtl ? layer.mirrored() : layer); move.add( _curve( parent, Interval(start, end, curve: chatStackRise), Interval(start, end, curve: chatStackFold), ), ); // A chat that was not drawn in the pile appears as it leaves it. fade.add( depth != null ? kAlwaysCompleteAnimation : _curve(parent, Interval(start, math.min(1, start + span * .5))), ); final labelStart = start + span * .45; label.add( _curve(parent, Interval(labelStart, end, curve: Curves.easeOut)), ); labelFrom.add( ChatStackPose(offset: Offset(rtl ? -8 : 8, to.offset.dy), scale: .96), ); } } final int count; final bool reduced; late final Animation scrim; /// The close button taking the pile's place. late final Animation close; final List from = []; final List target = []; final List> move = []; final List> fade = []; final List> label = []; final List labelFrom = []; final List _owned = []; Animation _curve( Animation parent, Curve curve, [ Curve? reverse, ]) { final curved = CurvedAnimation( parent: parent, curve: curve, reverseCurve: reverse, ); _owned.add(curved); return curved; } void dispose() { for (final curved in _owned) { curved.dispose(); } _owned.clear(); } }