Files
pipelines/app/lib/ui/motion/container_transform_source.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

146 lines
4.0 KiB
Dart

import 'package:flutter/widgets.dart';
/// Where a container transform starts and ends: an item's rectangle on the
/// screen and the surface it wears there.
@immutable
class ContainerTransformOrigin {
const ContainerTransformOrigin({
required this.rect,
required this.color,
this.borderRadius = BorderRadius.zero,
this.marker,
this.markerWidth = 0,
});
/// The item's bounds in global coordinates.
final Rect rect;
/// The item's opaque surface (its tint over the list's background).
final Color color;
final BorderRadius borderRadius;
/// The colour of the item's leading-edge marker, if it shows one.
final Color? marker;
final double markerWidth;
}
/// The items of one list that pages can grow from, by key.
class ContainerTransformSources {
final Map<Object, _ContainerTransformSourceState> _sources = {};
/// [key]'s current place and surface, or null when it is not laid out
/// (scrolled away, not built yet, or its list is gone).
ContainerTransformOrigin? originOf(Object? key) {
if (key == null) return null;
return _sources[key]?._origin();
}
/// A link a route can resolve [key] through each time it animates.
ContainerTransformLink link(Object? Function() key) =>
ContainerTransformLink(() => originOf(key()));
}
/// Resolves a route's origin lazily: the open item can change while the page
/// is up (another chat switched in), and closing shrinks into that one.
class ContainerTransformLink {
const ContainerTransformLink(this.resolve);
final ContainerTransformOrigin? Function() resolve;
}
/// Offers [ContainerTransformSources] to the items below it.
class ContainerTransformScope extends InheritedWidget {
const ContainerTransformScope({
super.key,
required this.sources,
required super.child,
});
final ContainerTransformSources sources;
static ContainerTransformSources? maybeOf(BuildContext context) =>
context.getInheritedWidgetOfExactType<ContainerTransformScope>()?.sources;
@override
bool updateShouldNotify(ContainerTransformScope oldWidget) =>
sources != oldWidget.sources;
}
/// Registers [child] as the item [id] a page can grow from and shrink into.
/// Without a [ContainerTransformScope] above it, it only builds [child].
class ContainerTransformSource extends StatefulWidget {
const ContainerTransformSource({
super.key,
required this.id,
required this.color,
this.borderRadius = BorderRadius.zero,
this.marker,
this.markerWidth = 0,
required this.child,
});
final Object id;
final Color color;
final BorderRadius borderRadius;
final Color? marker;
final double markerWidth;
final Widget child;
@override
State<ContainerTransformSource> createState() =>
_ContainerTransformSourceState();
}
class _ContainerTransformSourceState extends State<ContainerTransformSource> {
ContainerTransformSources? _sources;
Object? _id;
@override
void didChangeDependencies() {
super.didChangeDependencies();
_register();
}
@override
void didUpdateWidget(ContainerTransformSource oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.id != widget.id) _register();
}
void _register() {
_unregister();
final sources = ContainerTransformScope.maybeOf(context);
_sources = sources;
_id = widget.id;
sources?._sources[widget.id] = this;
}
void _unregister() {
final sources = _sources;
if (sources != null && sources._sources[_id] == this) {
sources._sources.remove(_id);
}
}
ContainerTransformOrigin? _origin() {
final box = context.findRenderObject();
if (box is! RenderBox || !box.attached || !box.hasSize) return null;
return ContainerTransformOrigin(
rect: box.localToGlobal(Offset.zero) & box.size,
color: widget.color,
borderRadius: widget.borderRadius,
marker: widget.marker,
markerWidth: widget.markerWidth,
);
}
@override
void dispose() {
_unregister();
super.dispose();
}
@override
Widget build(BuildContext context) => widget.child;
}