import 'package:flutter/material.dart'; import '../../../ui/theme/app_design_tokens.dart'; import 'chat_stack_disc.dart'; /// How near the close button's centre a dragged chat's pointer counts as /// over it: a little past its rim, as a thumb covers what it drags, and /// short of the first chat's slot a pitch above. const double kChatStackRemoveReach = kChatStackSlot * .7; /// How much the button grows while a dragged chat is over it. const double chatStackRemoveGrowth = 1.2; /// The open stack's close button, exactly where the pile was: the pile /// turns into a "×" as the column rises above it, and a tap folds the /// column back. /// /// While a chat is dragged it is also the remove target: its "×" turns the /// error colour ([armed]), and with the chat over it ([hover]) it grows and /// fills with the error colour. Dropping there is decided from the /// pointer's position, not by this widget. /// /// Everything moves through transitions (fade, turn, scale, decoration), so /// opening and dragging repaint it without rebuilding. class ChatStackCloseTarget extends StatelessWidget { const ChatStackCloseTarget({ super.key, required this.open, required this.armed, required this.hover, required this.reduced, required this.onClose, }); static const label = 'Close chats'; /// 0 folded (the pile shows), 1 open. final Animation open; /// 0 at rest, 1 while a chat is dragged. final Animation armed; /// 0 idle, 1 with a dragged chat over it. final Animation hover; /// Fades only: no turn, no growth. final bool reduced; final VoidCallback onClose; @override Widget build(BuildContext context) { final tokens = AppDesignTokens.of(context); final scheme = Theme.of(context).colorScheme; final dark = Theme.of(context).brightness == Brightness.dark; final shape = chatStackShape(tokens); final side = tokens.flat ? tokens.frameSide ?? BorderSide(color: scheme.outlineVariant) : BorderSide.none; final idle = ShapeDecoration( color: tokens.panelFill( scheme, dark ? scheme.surfaceContainerHighest : scheme.surface, ), shape: shape.copyWith(side: side), shadows: tokens.flat ? null : chatStackShadows(scheme), ); final hot = ShapeDecoration( color: scheme.error, shape: shape.copyWith( side: tokens.flat ? side.copyWith(color: scheme.error) : side, ), shadows: tokens.flat ? null : chatStackShadows(scheme), ); Widget icon(Color color, Animation opacity) => FadeTransition( opacity: opacity, child: Icon(Icons.close, color: color), ); Widget button = DecoratedBoxTransition( decoration: DecorationTween(begin: idle, end: hot).animate(hover), child: SizedBox.square( dimension: kChatStackDisc, child: Stack( alignment: Alignment.center, children: [ icon(scheme.onSurfaceVariant, ReverseAnimation(armed)), icon(scheme.error, armed), icon(scheme.onError, hover), ], ), ), ); final shown = open.drive(CurveTween(curve: Curves.easeOutCubic)); if (!reduced) { button = ScaleTransition( scale: Tween( begin: 1, end: chatStackRemoveGrowth, ).chain(CurveTween(curve: Curves.easeOutBack)).animate(hover), child: button, ); // The pile's top avatar turns into the "×" a quarter turn. button = RotationTransition( turns: Tween(begin: -.25, end: 0).animate(shown), child: ScaleTransition( scale: Tween(begin: .6, end: 1).animate(shown), child: button, ), ); } return Semantics( container: true, button: true, label: label, onTap: onClose, child: GestureDetector( behavior: HitTestBehavior.opaque, excludeFromSemantics: true, onTap: onClose, child: FadeTransition( opacity: shown, child: Center(child: button), ), ), ); } }