107 lines
3.3 KiB
Dart
107 lines
3.3 KiB
Dart
import 'dart:math' as math;
|
|
|
|
import 'package:flutter/animation.dart';
|
|
|
|
import 'chat_stack_disc.dart';
|
|
import 'chat_stack_pose.dart';
|
|
|
|
/// How long the stack takes to fan out into its column and to fold back.
|
|
const Duration chatStackOpenDuration = Duration(milliseconds: 280);
|
|
const Duration chatStackCloseDuration = Duration(milliseconds: 220);
|
|
|
|
/// Under reduced motion the column only fades in and out, in place.
|
|
const Duration chatStackFadeDuration = Duration(milliseconds: 120);
|
|
|
|
/// A short spring-like ease: a touch of overshoot on arrival, no bounce back.
|
|
const Curve chatStackRise = Cubic(.22, 1.18, .36, 1);
|
|
|
|
/// Folding plays the controller backwards: an ease-in in forward time leaves
|
|
/// the column briskly and settles gently into the pile.
|
|
const Curve chatStackFold = Curves.easeInCubic;
|
|
|
|
/// Every animation the open column reads, derived once from one controller
|
|
/// when the items change (never per frame). Item 0 is nearest the thumb; the
|
|
/// column rises from there, each item starting a little after the one below
|
|
/// it, and folds back top first.
|
|
class ChatStackMotion {
|
|
ChatStackMotion({
|
|
required Animation<double> parent,
|
|
required List<int?> depths,
|
|
required this.reduced,
|
|
required bool rtl,
|
|
}) : count = depths.length {
|
|
scrim = _curve(parent, const Interval(0, .7, curve: Curves.easeOut));
|
|
final span = count <= 1 ? 1.0 : .62;
|
|
final step = count <= 1 ? 0.0 : (1 - span) / (count - 1);
|
|
for (var i = 0; i < count; i++) {
|
|
final start = i * step;
|
|
final end = math.min(1.0, start + span);
|
|
final depth = depths[i];
|
|
final to = chatStackColumnPose(i, kChatStackPitch);
|
|
final layer = chatStackLayers[depth ?? chatStackLayers.length - 1];
|
|
target.add(to);
|
|
if (reduced) {
|
|
from.add(to);
|
|
move.add(parent);
|
|
fade.add(parent);
|
|
label.add(parent);
|
|
labelFrom.add(to);
|
|
continue;
|
|
}
|
|
from.add(rtl ? layer.mirrored() : layer);
|
|
move.add(
|
|
_curve(
|
|
parent,
|
|
Interval(start, end, curve: chatStackRise),
|
|
Interval(start, end, curve: chatStackFold),
|
|
),
|
|
);
|
|
// A chat that was not drawn in the pile appears as it leaves it.
|
|
fade.add(
|
|
depth != null
|
|
? kAlwaysCompleteAnimation
|
|
: _curve(parent, Interval(start, math.min(1, start + span * .5))),
|
|
);
|
|
final labelStart = start + span * .45;
|
|
label.add(
|
|
_curve(parent, Interval(labelStart, end, curve: Curves.easeOut)),
|
|
);
|
|
labelFrom.add(
|
|
ChatStackPose(offset: Offset(rtl ? -8 : 8, to.offset.dy), scale: .96),
|
|
);
|
|
}
|
|
}
|
|
|
|
final int count;
|
|
final bool reduced;
|
|
late final Animation<double> scrim;
|
|
final List<ChatStackPose> from = [];
|
|
final List<ChatStackPose> target = [];
|
|
final List<Animation<double>> move = [];
|
|
final List<Animation<double>> fade = [];
|
|
final List<Animation<double>> label = [];
|
|
final List<ChatStackPose> labelFrom = [];
|
|
final List<CurvedAnimation> _owned = [];
|
|
|
|
Animation<double> _curve(
|
|
Animation<double> parent,
|
|
Curve curve, [
|
|
Curve? reverse,
|
|
]) {
|
|
final curved = CurvedAnimation(
|
|
parent: parent,
|
|
curve: curve,
|
|
reverseCurve: reverse,
|
|
);
|
|
_owned.add(curved);
|
|
return curved;
|
|
}
|
|
|
|
void dispose() {
|
|
for (final curved in _owned) {
|
|
curved.dispose();
|
|
}
|
|
_owned.clear();
|
|
}
|
|
}
|