eea1361980
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.
265 lines
8.1 KiB
Dart
265 lines
8.1 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
import 'container_transform_source.dart';
|
|
import 'motion.dart';
|
|
|
|
/// Key of the growing surface, for finders.
|
|
const containerTransformSurfaceKey = ValueKey<String>(
|
|
'container-transform-surface',
|
|
);
|
|
|
|
/// A page growing out of its item's rectangle and shrinking back into it.
|
|
///
|
|
/// The surface morphs from the item's (its bounds, shape, tint and edge
|
|
/// marker) to the page's while the content cross-fades: on opening the item
|
|
/// is covered early and the page appears late; on closing the page goes
|
|
/// early and the item shows through late. The list behind never moves.
|
|
///
|
|
/// Without an [origin] (reduced motion, or the item is not on screen) the
|
|
/// page simply fades. The widget tree is the same in every state, so the
|
|
/// page keeps its state at rest and through any transition.
|
|
class ContainerTransformFlight extends StatefulWidget {
|
|
const ContainerTransformFlight({
|
|
super.key,
|
|
required this.animation,
|
|
required this.origin,
|
|
required this.closing,
|
|
required this.tracking,
|
|
required this.pageColor,
|
|
required this.scrimColor,
|
|
required this.child,
|
|
});
|
|
|
|
/// The route's animation: 0 at the item, 1 as the full page.
|
|
final Animation<double> animation;
|
|
|
|
/// Resolves where to grow from or shrink into; null fades instead.
|
|
final ContainerTransformOrigin? Function()? origin;
|
|
|
|
/// Whether the page is on its way back into the item.
|
|
final bool Function() closing;
|
|
|
|
/// Whether a finger drives the animation (it then follows linearly).
|
|
final bool Function() tracking;
|
|
final Color pageColor;
|
|
final Color scrimColor;
|
|
final Widget child;
|
|
|
|
@override
|
|
State<ContainerTransformFlight> createState() =>
|
|
_ContainerTransformFlightState();
|
|
}
|
|
|
|
class _ContainerTransformFlightState extends State<ContainerTransformFlight> {
|
|
ContainerTransformOrigin? _origin;
|
|
bool _resolved = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
widget.animation.addStatusListener(_statusChanged);
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(ContainerTransformFlight oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.animation != widget.animation) {
|
|
oldWidget.animation.removeStatusListener(_statusChanged);
|
|
widget.animation.addStatusListener(_statusChanged);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.animation.removeStatusListener(_statusChanged);
|
|
super.dispose();
|
|
}
|
|
|
|
// Each flight measures its item afresh: the open item may have changed,
|
|
// or the window may have resized, while the page was up.
|
|
void _statusChanged(AnimationStatus status) {
|
|
if (status.isCompleted || status.isDismissed) _resolved = false;
|
|
}
|
|
|
|
ContainerTransformOrigin? _resolve() {
|
|
if (!_resolved) {
|
|
_resolved = true;
|
|
_origin = widget.origin?.call();
|
|
}
|
|
return _origin;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final size = constraints.biggest;
|
|
return AnimatedBuilder(
|
|
animation: widget.animation,
|
|
child: widget.child,
|
|
builder: (context, child) {
|
|
final t = widget.animation.value;
|
|
final atRest = widget.animation.isCompleted;
|
|
final origin = atRest ? null : _resolve();
|
|
final frame = origin == null
|
|
? _Frame.fade(t, size, widget.pageColor)
|
|
: _Frame.transform(
|
|
t,
|
|
_local(origin.rect),
|
|
size,
|
|
origin,
|
|
widget.pageColor,
|
|
closing: widget.closing(),
|
|
tracking: widget.tracking(),
|
|
);
|
|
// The list dims as the page grows and brightens as it shrinks.
|
|
final scrim = atRest ? 0.0 : Motion.containerTransformScrim * t;
|
|
return Stack(
|
|
fit: StackFit.expand,
|
|
children: [
|
|
IgnorePointer(
|
|
child: ColoredBox(
|
|
color: widget.scrimColor.withValues(
|
|
alpha: widget.scrimColor.a * scrim,
|
|
),
|
|
),
|
|
),
|
|
Positioned.fromRect(
|
|
rect: frame.rect,
|
|
child: Opacity(
|
|
opacity: frame.surfaceOpacity,
|
|
child: ClipRRect(
|
|
key: containerTransformSurfaceKey,
|
|
borderRadius: frame.radius,
|
|
clipBehavior: atRest ? Clip.none : Clip.hardEdge,
|
|
child: DecoratedBox(
|
|
decoration: BoxDecoration(color: frame.color),
|
|
position: DecorationPosition.background,
|
|
child: _Content(
|
|
size: size,
|
|
opacity: frame.contentOpacity,
|
|
marker: frame.marker,
|
|
markerWidth: frame.markerWidth,
|
|
child: child!,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
|
|
/// [global] in this page's coordinates; the page fills its navigator.
|
|
Rect _local(Rect global) {
|
|
final box = context.findRenderObject();
|
|
if (box is! RenderBox || !box.hasSize) return global;
|
|
return global.shift(-box.localToGlobal(Offset.zero));
|
|
}
|
|
}
|
|
|
|
/// The page laid out at its full size, aligned to the top of the surface
|
|
/// and scaled to its width, so nothing inside relayouts as it grows.
|
|
class _Content extends StatelessWidget {
|
|
const _Content({
|
|
required this.size,
|
|
required this.opacity,
|
|
required this.marker,
|
|
required this.markerWidth,
|
|
required this.child,
|
|
});
|
|
|
|
final Size size;
|
|
final double opacity;
|
|
final Color? marker;
|
|
final double markerWidth;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => DecoratedBox(
|
|
position: DecorationPosition.foreground,
|
|
decoration: BoxDecoration(
|
|
border: marker == null || markerWidth <= 0
|
|
? null
|
|
: BorderDirectional(
|
|
start: BorderSide(color: marker!, width: markerWidth),
|
|
),
|
|
),
|
|
child: FittedBox(
|
|
fit: BoxFit.fitWidth,
|
|
alignment: Alignment.topCenter,
|
|
clipBehavior: Clip.none,
|
|
child: SizedBox.fromSize(
|
|
size: size,
|
|
child: Opacity(opacity: opacity, child: child),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
class _Frame {
|
|
const _Frame({
|
|
required this.rect,
|
|
required this.radius,
|
|
required this.color,
|
|
required this.surfaceOpacity,
|
|
required this.contentOpacity,
|
|
this.marker,
|
|
this.markerWidth = 0,
|
|
});
|
|
|
|
/// The whole page fading in place.
|
|
factory _Frame.fade(double t, Size size, Color pageColor) => _Frame(
|
|
rect: Offset.zero & size,
|
|
radius: BorderRadius.zero,
|
|
color: pageColor,
|
|
surfaceOpacity: Curves.easeInOut.transform(t),
|
|
contentOpacity: 1,
|
|
);
|
|
|
|
factory _Frame.transform(
|
|
double t,
|
|
Rect start,
|
|
Size size,
|
|
ContainerTransformOrigin origin,
|
|
Color pageColor, {
|
|
required bool closing,
|
|
required bool tracking,
|
|
}) {
|
|
const curve = Curves.fastOutSlowIn;
|
|
// Played backwards in time, the same easing reads as the item settling.
|
|
final grown = tracking
|
|
? t
|
|
: closing
|
|
? curve.flipped.transform(t)
|
|
: curve.transform(t);
|
|
// Opening covers the item first and shows the page late; closing hides
|
|
// the page first and uncovers the item late.
|
|
final surface = closing
|
|
? const Interval(0, 0.3).transform(t)
|
|
: const Interval(0, 0.2).transform(t);
|
|
final content = closing
|
|
? const Interval(0.7, 1).transform(t)
|
|
: const Interval(0.25, 0.7, curve: Curves.easeIn).transform(t);
|
|
return _Frame(
|
|
rect: Rect.lerp(start, Offset.zero & size, grown)!,
|
|
radius: BorderRadius.lerp(origin.borderRadius, BorderRadius.zero, grown)!,
|
|
color: Color.lerp(origin.color, pageColor, grown)!,
|
|
surfaceOpacity: surface,
|
|
contentOpacity: content,
|
|
marker: origin.marker,
|
|
markerWidth: origin.markerWidth * (1 - grown),
|
|
);
|
|
}
|
|
|
|
final Rect rect;
|
|
final BorderRadius radius;
|
|
final Color color;
|
|
final double surfaceOpacity;
|
|
final double contentOpacity;
|
|
final Color? marker;
|
|
final double markerWidth;
|
|
}
|