Files
pipelines/app/lib/ui/motion/container_transform_gestures.dart
T
alan eea1361980 Grow the tapped chat row into the chat page on phones
Replace the identity-avatar Hero with a container transform: the row's
surface, tint and edge marker morph into the full page while the content
cross-fades, and Back, the Cupertino edge swipe or Android predictive back
shrink the page into the open chat's row. Reduced motion or a row off
screen fades instead; wide layouts are unchanged.
2026-11-10 03:11:05 +00:00

182 lines
5.0 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/gestures.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
/// What a back gesture needs from the page it closes.
abstract interface class ContainerTransformGestureRoute
implements PredictiveBackRoute {
/// Whether a back gesture may start now: the page is current, at rest and
/// would really pop.
bool get backGestureEnabled;
/// A finger took the page from the start edge.
void startSwipeBack();
/// The finger moved by [fraction] of the page's width toward the end edge.
void updateSwipeBack(double fraction);
/// The finger let go at [velocity] page widths per second.
void endSwipeBack(double velocity);
}
/// The back gestures of a page that shrinks into its item: on a Cupertino
/// host the native swipe from the start edge; on Android the system's
/// predictive back. Either one drives the page's own animation, so the
/// shrink follows the finger, and releasing commits or cancels it.
class ContainerTransformBackGestures extends StatelessWidget {
const ContainerTransformBackGestures({
super.key,
required this.route,
required this.cupertino,
required this.child,
});
final ContainerTransformGestureRoute route;
final bool cupertino;
final Widget child;
@override
Widget build(BuildContext context) => cupertino
? _EdgeSwipeBack(route: route, child: child)
: _PredictiveBack(route: route, child: child);
}
/// The width of the start edge that takes a swipe back, as in iOS.
const double _edgeWidth = 20;
class _EdgeSwipeBack extends StatefulWidget {
const _EdgeSwipeBack({required this.route, required this.child});
final ContainerTransformGestureRoute route;
final Widget child;
@override
State<_EdgeSwipeBack> createState() => _EdgeSwipeBackState();
}
class _EdgeSwipeBackState extends State<_EdgeSwipeBack> {
late final HorizontalDragGestureRecognizer _recognizer =
HorizontalDragGestureRecognizer(debugOwner: this)
..onStart = _start
..onUpdate = _update
..onEnd = _end
..onCancel = _cancel;
bool _dragging = false;
@override
void dispose() {
_recognizer.dispose();
if (_dragging) widget.route.endSwipeBack(0);
super.dispose();
}
double _logical(double value) =>
Directionality.of(context) == TextDirection.rtl ? -value : value;
void _start(DragStartDetails details) {
_dragging = true;
widget.route.startSwipeBack();
}
void _update(DragUpdateDetails details) {
if (!_dragging) return;
widget.route.updateSwipeBack(
_logical(details.primaryDelta! / context.size!.width),
);
}
void _end(DragEndDetails details) {
if (!_dragging) return;
_dragging = false;
widget.route.endSwipeBack(
_logical(details.velocity.pixelsPerSecond.dx / context.size!.width),
);
}
void _cancel() {
if (!_dragging) return;
_dragging = false;
widget.route.endSwipeBack(0);
}
@override
Widget build(BuildContext context) {
final padding = MediaQuery.paddingOf(context);
final notch = Directionality.of(context) == TextDirection.rtl
? padding.right
: padding.left;
return Stack(
fit: StackFit.passthrough,
children: [
widget.child,
PositionedDirectional(
start: 0,
top: 0,
bottom: 0,
width: math.max(notch, _edgeWidth),
child: Listener(
behavior: HitTestBehavior.translucent,
onPointerDown: (event) {
if (widget.route.backGestureEnabled) {
_recognizer.addPointer(event);
}
},
),
),
],
);
}
}
/// Forwards Android's predictive back to the page, as the stock Material
/// page transition does, without that transition's own shrink.
class _PredictiveBack extends StatefulWidget {
const _PredictiveBack({required this.route, required this.child});
final ContainerTransformGestureRoute route;
final Widget child;
@override
State<_PredictiveBack> createState() => _PredictiveBackState();
}
class _PredictiveBackState extends State<_PredictiveBack>
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
bool handleStartBackGesture(PredictiveBackEvent backEvent) {
if (backEvent.isButtonEvent || !widget.route.backGestureEnabled) {
return false;
}
widget.route.handleStartBackGesture(progress: 1 - backEvent.progress);
return true;
}
@override
void handleUpdateBackGestureProgress(PredictiveBackEvent backEvent) => widget
.route
.handleUpdateBackGestureProgress(progress: 1 - backEvent.progress);
@override
void handleCancelBackGesture() => widget.route.handleCancelBackGesture();
@override
void handleCommitBackGesture() => widget.route.handleCommitBackGesture();
@override
Widget build(BuildContext context) => widget.child;
}