import 'dart:ui' show lerpDouble; import 'package:flutter/animation.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'chat_stack_drop_target.dart'; part 'chat_stack_drag_types.dart'; /// One drag in the open column: which item is lifted, the slot it would /// drop into or the item it would drop onto, whether it is over the remove /// target, and the motion of every item. It notifies only when a drag /// starts or ends (the column then rebuilds once); each pointer move only /// repaints the items that move. class ChatStackDrag extends ChangeNotifier { ChatStackDrag({required TickerProvider vsync, required this.pitch}) : _vsync = vsync { lift = AnimationController(vsync: vsync, duration: chatStackShiftDuration) ..addListener(_paintLifted); settle = AnimationController(vsync: vsync, duration: chatStackSettleDuration) ..addListener(_paintLifted) ..addStatusListener(_settled); removeShown = AnimationController( vsync: vsync, duration: chatStackRemoveDuration, ); hover = AnimationController(vsync: vsync, duration: chatStackHoverDuration) ..addListener(_paintLifted); _ontoAny = AnimationController( vsync: vsync, duration: chatStackHoverDuration, )..addListener(_paintLifted); } /// 1 while the item is over another's middle: it shrinks onto it, so the /// target's grown ring shows around it. late final AnimationController _ontoAny; final TickerProvider _vsync; /// The distance between neighbouring slots. final double pitch; /// Under reduced motion neighbours swap at once and nothing slides. bool reduced = false; late final AnimationController lift; late final AnimationController settle; /// The remove target arming, and its highlight while the item is over it. late final AnimationController removeShown; late final AnimationController hover; final Map _items = {}; final Map _shifts = {}; final Map _glows = {}; /// The item being dragged or settling; it paints above the others. String? get lifted => _lifted; String? _lifted; /// The items' order when the drag started; null when none is dragged. List? get order => _order; List? _order; int _from = 0; int? _slot; int? _onto; bool Function(int index)? _canDropOnto; int? _lastSlot; bool _over = false; Offset _pointer = Offset.zero; Offset _residual = Offset.zero; bool get dragging => _order != null; bool get overRemove => _over; /// The item the dragged one would drop onto, if any. String? get ontoKey => _onto == null ? null : _order?[_onto!]; /// Where [key]'s item is painted; created on first use. ChatStackDragItem item(String key) => _items.putIfAbsent(key, ChatStackDragItem.new); /// 0 at rest, 1 while the dragged item is over [key]'s (to drop onto it). Animation glow(String key) => _glows.putIfAbsent(key, () { final glow = AnimationController( vsync: _vsync, duration: chatStackHoverDuration, ); glow.addListener(() => _paint(key)); return glow; }); static const _curve = Curves.easeOutCubic; /// Lifts [key] out of [order] (the items, without Add chat). A drop on the /// middle of an item [canDropOnto] accepts lands on it; the gap never /// goes past [lastSlot] (an item after it is a target only). void start( String key, List order, { bool Function(int index)? canDropOnto, int? lastSlot, }) { final from = order.indexOf(key); if (from < 0) return; if (_lifted != null) _finishSettle(); _order = List.of(order); _from = from; _slot = from; _onto = null; _canDropOnto = canDropOnto; _lastSlot = lastSlot; _over = false; _pointer = Offset.zero; _residual = Offset.zero; _lifted = key; HapticFeedback.mediumImpact(); if (reduced) { lift.value = 1; removeShown.value = 1; } else { lift.forward(from: 0); removeShown.forward(); } hover.value = 0; notifyListeners(); } /// The pointer moved [fromOrigin] since the press; [overRemove] says /// whether it is over the remove target. void move(Offset fromOrigin, {required bool overRemove}) { final order = _order; if (order == null) return; _pointer = fromOrigin; if (overRemove != _over) { _over = overRemove; if (overRemove) HapticFeedback.selectionClick(); _run(hover, overRemove ? 1 : 0); } // Far to the side of the column, or over the remove target, there is no // slot: the gap closes and a drop there puts the item back. int? slot; int? onto; if (!overRemove && fromOrigin.dx.abs() <= pitch * 1.5) { final target = chatStackDropTarget( position: _from - fromOrigin.dy / pitch, from: _from, count: order.length, slot: _slot ?? _from, canDropOnto: _canDropOnto, lastSlot: _lastSlot, ); slot = target.slot; onto = target.onto; } if (onto != _onto) { final before = _onto; _onto = onto; if (onto != null) HapticFeedback.selectionClick(); if (before != null) _run(glow(order[before]) as AnimationController, 0); if (onto != null) _run(glow(order[onto]) as AnimationController, 1); _run(_ontoAny, onto == null ? 0 : 1); } if (slot != _slot) { if (slot != null && _slot != null) HapticFeedback.selectionClick(); _slot = slot; _shiftNeighbours(animate: !reduced); } _paintLifted(); } /// Ends the drag and says what the drop does; the motion back (or into /// the new slot) runs from here. ChatStackDrop end() { final order = _order; if (order == null) return const ChatStackDropBack(); final slot = _slot; final onto = _onto; final ChatStackDrop drop = _over ? const ChatStackDropRemove() : onto != null ? ChatStackDropOnto(onto) : slot != null && slot != _from ? ChatStackDropMove(slot) : const ChatStackDropBack(); _order = null; _slot = null; switch (drop) { case ChatStackDropMove(:final index): // The new order puts the neighbours where they are now drawn; the // dragged item glides from the finger into its new slot. _residual = _pointer + Offset(0, (index - _from) * pitch); _shiftNeighbours(animate: false); case ChatStackDropRemove() || ChatStackDropOnto(): // It joins the target (or leaves): no glide back. _residual = Offset.zero; _shiftNeighbours(animate: false); case ChatStackDropBack(): _residual = _pointer; _shiftNeighbours(animate: !reduced); } _release(clearGlowAtOnce: drop is ChatStackDropOnto); return drop; } /// Puts the item back where it was (Escape, a cancelled pointer, the /// column folding). void cancel() { if (_order == null) return; _order = null; _slot = null; _residual = _pointer; _shiftNeighbours(animate: !reduced); _release(); } void _release({bool clearGlowAtOnce = false}) { _pointer = Offset.zero; if (_over) HapticFeedback.selectionClick(); _over = false; _onto = null; clearGlowAtOnce ? _ontoAny.value = 0 : _run(_ontoAny, 0); for (final glow in _glows.values) { clearGlowAtOnce ? glow.value = 0 : _run(glow, 0); } if (reduced) { hover.value = 0; removeShown.value = 0; lift.value = 0; _residual = Offset.zero; _finishSettle(); return; } hover.reverse(); removeShown.reverse(); lift.reverse(); settle.forward(from: 0); } void _run(AnimationController controller, double target) { if (reduced) { controller.value = target; } else { controller.animateTo(target, curve: _curve); } } void _settled(AnimationStatus status) { if (status == AnimationStatus.completed) _finishSettle(); } void _finishSettle() { settle.stop(); final key = _lifted; _residual = Offset.zero; if (key != null) item(key)._set(Offset.zero, 1); if (_order != null) return; _lifted = null; notifyListeners(); } void _paintLifted() { final key = _lifted; if (key == null) return; // Over the remove target the item shrinks into it, so the target's lit // rim shows around it. final scale = lerpDouble(1, chatStackLiftScale, _curve.transform(lift.value))! * lerpDouble(1, chatStackOverRemoveScale, hover.value)! * lerpDouble(1, chatStackOverRemoveScale, _ontoAny.value)!; final offset = _order != null ? _pointer : _residual * (1 - _curve.transform(settle.value)); item(key)._set(offset, scale); } /// Paints a neighbour: slid by its shift, grown by its glow. void _paint(String key) { if (key == _lifted) return; final shift = _shifts[key]?.value ?? 0; final glow = _glows[key]?.value ?? 0; item( key, )._set(Offset(0, shift * pitch), lerpDouble(1, chatStackOntoScale, glow)!); } /// Slides each neighbour aside to open the gap at the current slot, or /// back home when there is none. void _shiftNeighbours({required bool animate}) { final order = _order ?? const []; final keys = {...order, ..._shifts.keys}; final slot = _slot; for (final key in keys) { if (key == _lifted && _order != null) continue; final i = order.indexOf(key); var target = 0.0; if (slot != null && i >= 0) { // Down (toward the thumb) when the lifted item goes above it, up // when it goes below. if (_from < slot && i > _from && i <= slot) target = 1; if (slot < _from && i >= slot && i < _from) target = -1; } final shift = _shifts.putIfAbsent(key, () { final shift = AnimationController.unbounded(vsync: _vsync); shift.addListener(() => _paint(key)); return shift; }); if (shift.value == target) continue; if (animate) { shift.animateTo( target, duration: chatStackShiftDuration, curve: _curve, ); } else { shift ..stop() ..value = target; } } } @override void dispose() { lift.dispose(); settle.dispose(); removeShown.dispose(); hover.dispose(); _ontoAny.dispose(); for (final controller in [..._shifts.values, ..._glows.values]) { controller.dispose(); } for (final item in _items.values) { item.dispose(); } super.dispose(); } }