Animate mobile chat swaps as overlapping raised cards

This commit is contained in:
2026-11-07 12:38:11 +00:00
parent 414a74e0c9
commit 19958e81c5
5 changed files with 125 additions and 7 deletions
@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';
/// A raised foreground while phone chats cross. The transcript still lays out
/// at its real animated height, so the settled peek can reach its final row.
class ChatPaneSwapCard extends StatelessWidget {
const ChatPaneSwapCard({
super.key,
required this.enabled,
required this.lift,
required this.child,
});
final bool enabled;
final double lift;
final Widget child;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final amount = enabled ? lift : 0.0;
final radius = BorderRadius.circular(16 * amount);
return DecoratedBox(
decoration: BoxDecoration(
color: enabled ? theme.colorScheme.surface : null,
borderRadius: radius,
boxShadow: [
BoxShadow(
color: theme.shadowColor.withValues(alpha: .24 * amount),
blurRadius: 24 * amount,
spreadRadius: 2 * amount,
offset: Offset(0, -6 * amount),
),
],
),
child: ClipRRect(borderRadius: radius, child: child),
);
}
}
@@ -1,9 +1,10 @@
import 'dart:math' as math;
import 'dart:ui' show lerpDouble;
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
/// One continuous phone-pane swap. Both rectangles and both chrome reveals
/// follow the same ease; an interrupted swap starts at their current values.
/// One continuous phone-pane swap. Both cards travel together; the incoming card grows
/// ahead of the outgoing card shrinking. Chrome follows the shared ease; an interrupted swap starts at their current values.
/// The app's scheduler speed preference scales this ticker once globally.
class ChatPaneSwitchMotion {
ChatPaneSwitchMotion({required TickerProvider vsync})
@@ -13,7 +14,7 @@ class ChatPaneSwitchMotion {
value: 1,
);
static const Duration duration = Duration(milliseconds: 360);
static const Duration duration = Duration(milliseconds: 480);
static const Curve curve = Cubic(.22, .7, .24, 1);
final AnimationController _controller;
Animation<double> get animation => _controller.view;
@@ -23,6 +24,8 @@ class ChatPaneSwitchMotion {
Map<String, double> _barFrom = const {};
Map<String, double> _barTo = const {};
Size? _size;
String? _incomingId;
Map<String, double> _liftFrom = const {};
bool get isAnimating => _controller.isAnimating;
double get _progress => curve.transform(_controller.value);
@@ -42,12 +45,16 @@ class ChatPaneSwitchMotion {
_size = size;
if (travel) {
final ids = {..._to.keys, ...rects.keys};
_liftFrom = {for (final id in ids) id: liftFor(id)};
_from = {for (final id in ids) id: rectFor(id) ?? rects[id]!};
_barFrom = {for (final id in ids) id: _reveal(id)};
_incomingId = activeId;
_to = rects;
_barTo = {for (final id in rects.keys) id: id == activeId ? 1 : 0};
_controller.forward(from: 0);
} else if (!mapEquals(rects, _to) || !animate) {
_incomingId = null;
_liftFrom = const {};
_from = rects;
_to = rects;
_barFrom = _barTo = {
@@ -62,12 +69,34 @@ class ChatPaneSwitchMotion {
_from = _to = const {};
_barFrom = _barTo = const {};
_size = null;
_incomingId = null;
_liftFrom = const {};
_controller.value = 1;
}
Rect? rectFor(String id) {
final to = _to[id];
return to == null ? null : Rect.lerp(_from[id] ?? to, to, _progress);
if (to == null) return null;
final from = _from[id] ?? to;
final t = _progress;
// Separate travel from resizing: overlapping cards should read as passing
// over/under each other rather than collapsing into the same small strip.
final sizeT = id == _incomingId ? Curves.easeOut.transform(t) : t * t;
return Rect.fromLTWH(
lerpDouble(from.left, to.left, t)!,
lerpDouble(from.top, to.top, t)!,
lerpDouble(from.width, to.width, t)!,
lerpDouble(from.height, to.height, sizeT)!,
);
}
/// A short lift on the foreground card; interruptions keep the current
/// elevation before handing the foreground to the newly selected pane.
double liftFor(String id) {
final t = _controller.value;
final previous = (_liftFrom[id] ?? 0) * (1 - _progress);
final lift = id == _incomingId ? math.sin(math.pi * t) : 0.0;
return (previous + lift).clamp(0.0, 1.0);
}
void dispose() => _controller.dispose();
@@ -9,6 +9,7 @@ import 'chat_workspace_geometry.dart';
import 'chat_mobile_panes.dart';
import 'chat_pane_switch_motion.dart';
import 'chat_pane_backdrop.dart';
import 'chat_pane_swap_card.dart';
export 'chat_workspace_controller.dart';
export 'chat_workspace_geometry.dart' show ChatPaneDirection;
@@ -196,7 +197,11 @@ class _ChatWorkspaceState extends State<ChatWorkspace>
(collapsed
? Offset.zero & constraints.biggest
: geometry.panes[pane.id]!),
child: child!,
child: ChatPaneSwapCard(
enabled: mobile != null,
lift: _motion.liftFor(pane.id),
child: child!,
),
),
),
if (!collapsed && mobile == null)