import 'package:flutter/widgets.dart'; import '../../../ui/motion/motion.dart'; /// The messages this device has just sent that have not yet been drawn. /// /// A send marks its operation id; the first [ChatSentEntrance] built for that /// id claims it and plays. Everything else (history, a scroll back into view, /// another device's message, the server's copy replacing the local one, a /// remount) finds nothing to claim and simply appears. class ChatSentEntrances { final Set _pending = {}; /// Marks [operationId] as just sent, so its row enters once. void mark(String operationId) { if (operationId.isNotEmpty) _pending.add(operationId); } /// Forgets [operationId] (a refused send) without playing it. void forget(String operationId) => _pending.remove(operationId); /// True once for a marked [operationId]. bool claim(String operationId) => _pending.remove(operationId); } /// One transcript row that rises from the composer when it is a message the /// operator has just sent: up [Motion.sentMessageRise], growing from /// [Motion.sentMessageScaleBegin] and fading in. Paint-only, so the row is /// laid out (and scrolled to) at its resting place from the first frame. /// /// Whether it plays is decided once, when it is first built; under reduced /// motion it never plays. class ChatSentEntrance extends StatefulWidget { const ChatSentEntrance({ super.key, required this.entrances, required this.operationId, required this.child, }); final ChatSentEntrances entrances; /// The row's message operation id, or empty for a row that is not a sent /// message. final String operationId; final Widget child; @override State createState() => _ChatSentEntranceState(); } class _ChatSentEntranceState extends State with SingleTickerProviderStateMixin { AnimationController? _controller; Animation _motion = kAlwaysCompleteAnimation; Animation _opacity = kAlwaysCompleteAnimation; bool _decided = false; @override void didChangeDependencies() { super.didChangeDependencies(); if (_decided) return; _decided = true; if (widget.operationId.isEmpty || !widget.entrances.claim(widget.operationId)) { return; } final duration = Motion.of(context, Motion.sentMessageDuration); if (duration == Duration.zero) return; // Reduced motion is already settled through the MediaQuery above, so the // controller does not shorten itself a second time. final controller = AnimationController( vsync: this, duration: duration, animationBehavior: AnimationBehavior.preserve, ); _controller = controller; _motion = CurvedAnimation( parent: controller, curve: Motion.sentMessageCurve, ); _opacity = CurvedAnimation( parent: controller, curve: const Interval( 0, Motion.sentMessageOpacityPortion, curve: Curves.easeOut, ), ); controller.forward(); } @override void dispose() { if (_motion case final CurvedAnimation curved) curved.dispose(); if (_opacity case final CurvedAnimation curved) curved.dispose(); _controller?.dispose(); super.dispose(); } // The same tree whether or not it plays, so the row's element (and any // state in it) is never remounted. At rest the transforms are identities // and the opacity is full, which paint the row directly. @override Widget build(BuildContext context) => FadeTransition( opacity: _opacity, alwaysIncludeSemantics: true, child: AnimatedBuilder( animation: _motion, child: widget.child, builder: (context, child) { final t = _motion.value; return Transform.translate( offset: Offset(0, (1 - t) * Motion.sentMessageRise), child: Transform.scale( scale: Motion.sentMessageScaleBegin + (1 - Motion.sentMessageScaleBegin) * t, alignment: AlignmentDirectional.bottomEnd, child: child, ), ); }, ), ); }