Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Implement stacked faces timeline animations

laurenspriem36a05cc07917
diff --git a/mobile/apps/photos/lib/ui/viewer/people/faces_timeline_page.dart b/mobile/apps/photos/lib/ui/viewer/people/faces_timeline_page.dart
index ef6b77ec03..8d5d626181 100644
--- a/mobile/apps/photos/lib/ui/viewer/people/faces_timeline_page.dart
+++ b/mobile/apps/photos/lib/ui/viewer/people/faces_timeline_page.dart
@@ -1,5 +1,7 @@
import "dart:async";
+import "dart:math" as math;
import "dart:typed_data";
+import "dart:ui";
import "package:collection/collection.dart";
import "package:flutter/material.dart";
@@ -12,6 +14,7 @@ import "package:photos/models/faces_timeline/faces_timeline_models.dart";
import "package:photos/models/ml/face/face.dart";
import "package:photos/models/ml/face/person.dart";
import "package:photos/services/faces_timeline/faces_timeline_service.dart";
+import "package:photos/theme/colors.dart";
import "package:photos/theme/effects.dart";
import "package:photos/theme/ente_theme.dart";
import "package:photos/utils/face/face_thumbnail_cache.dart";
@@ -28,10 +31,14 @@ class FacesTimelinePage extends StatefulWidget {
class _FacesTimelinePageState extends State<FacesTimelinePage>
with TickerProviderStateMixin {
static const _frameInterval = Duration(milliseconds: 1000);
+ static const _cardTransitionDuration = Duration(milliseconds: 520);
final Logger _logger = Logger("FacesTimelinePage");
- int _frameViewVersion = 0;
- _TransitionDirection _transitionDirection = _TransitionDirection.forward;
+ late final AnimationController _cardTransitionController;
+ double _stackProgress = 0;
+ double _animationStartProgress = 0;
+ int _targetIndex = 0;
+ bool _isAnimatingCard = false;
late Future<List<_TimelineFrame>> _framesFuture;
final List<_TimelineFrame> _frames = [];
@@ -49,12 +56,22 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
@override
void initState() {
super.initState();
+ _cardTransitionController = AnimationController(
+ vsync: this,
+ duration: _cardTransitionDuration,
+ )
+ ..addListener(_onCardAnimationTick)
+ ..addStatusListener(_onCardAnimationStatusChanged);
_framesFuture = _loadFrames();
}
@override
void dispose() {
_playTimer?.cancel();
+ _cardTransitionController
+ ..removeListener(_onCardAnimationTick)
+ ..removeStatusListener(_onCardAnimationStatusChanged)
+ ..dispose();
super.dispose();
}
@@ -76,8 +93,11 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
..addAll(frames);
_currentIndex = 0;
_sliderValue = 0;
- _frameViewVersion = 0;
- _transitionDirection = _TransitionDirection.forward;
+ _stackProgress = 0;
+ _animationStartProgress = 0;
+ _targetIndex = 0;
+ _isAnimatingCard = false;
+ _cardTransitionController.value = 0;
_currentCaptionValue = frames.first.captionValue;
_previousCaptionValue = _currentCaptionValue;
_currentCaptionType = frames.first.captionType;
@@ -176,7 +196,7 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
}
void _showNextFrame() {
- if (_frames.isEmpty) return;
+ if (_frames.isEmpty || _isAnimatingCard) return;
if (_currentIndex >= _frames.length - 1) {
_pausePlayback();
return;
@@ -186,27 +206,40 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
}
void _setCurrentFrame(int index) {
- if (index < 0 || index >= _frames.length) {
+ _animateToIndex(index);
+ }
+
+ void _animateToIndex(int index) {
+ if (_frames.isEmpty) {
return;
}
- final frame = _frames[index];
- if (_currentIndex == index) {
+ final clamped = index.clamp(0, _frames.length - 1);
+ final targetProgress = clamped.toDouble();
+ if (!_isAnimatingCard && clamped == _currentIndex) {
setState(() {
- _sliderValue = index.toDouble();
+ _sliderValue = targetProgress;
});
return;
}
- final direction = _directionForIndex(index);
+
+ if (_isAnimatingCard && _targetIndex == clamped) {
+ return;
+ }
+
+ _animationStartProgress = _stackProgress;
+ _targetIndex = clamped;
+ _isAnimatingCard = true;
+ final distance = (targetProgress - _animationStartProgress).abs();
+ final multiplier = distance.clamp(1.0, 4.0);
+ _cardTransitionController.duration = Duration(
+ milliseconds:
+ (_cardTransitionDuration.inMilliseconds * multiplier).round(),
+ );
+ _cardTransitionController
+ ..reset()
+ ..forward();
setState(() {
- if (_currentIndex != index) {
- _previousCaptionValue = _currentCaptionValue;
- _currentCaptionValue = frame.captionValue;
- _currentCaptionType = frame.captionType;
- _currentIndex = index;
- _frameViewVersion++;
- _transitionDirection = direction;
- }
- _sliderValue = index.toDouble();
+ _sliderValue = targetProgress;
});
}
@@ -279,68 +312,15 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
),
);
}
+ final displayIndex =
+ _stackProgress.round().clamp(0, frames.length - 1);
return Column(
children: [
Expanded(
child: Stack(
children: [
Positioned.fill(
- child: AnimatedSwitcher(
- duration: const Duration(milliseconds: 500),
- reverseDuration:
- const Duration(milliseconds: 450),
- switchInCurve: Curves.easeOutCubic,
- switchOutCurve: Curves.easeInCubic,
- layoutBuilder: (currentChild, previousChildren) {
- return Stack(
- alignment: Alignment.center,
- children: [
- if (currentChild != null) currentChild,
- ...previousChildren,
- ],
- );
- },
- transitionBuilder: (child, animation) {
- final isIncoming = child.key ==
- ValueKey<int>(_frameViewVersion);
- if (isIncoming) {
- final curved = CurvedAnimation(
- parent: animation,
- curve: Curves.easeOutCubic,
- );
- final beginOffset = _transitionDirection ==
- _TransitionDirection.forward
- ? const Offset(0, -0.35)
- : const Offset(0, 0.8);
- final position = Tween<Offset>(
- begin: beginOffset,
- end: Offset.zero,
- ).animate(curved);
- return SlideTransition(
- position: position,
- child: child,
- );
- } else {
- final curved = CurvedAnimation(
- parent: ReverseAnimation(animation),
- curve: Curves.easeInCubic,
- );
- final targetOffset = _transitionDirection ==
- _TransitionDirection.forward
- ? const Offset(0, 1.05)
- : const Offset(0, -1.05);
- final position = Tween<Offset>(
- begin: Offset.zero,
- end: targetOffset,
- ).animate(curved);
- return SlideTransition(
- position: position,
- child: child,
- );
- }
- },
- child: _buildFrameView(context),
- ),
+ child: _buildFrameView(context),
),
Positioned(
top: 24,
@@ -357,7 +337,7 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
borderRadius: BorderRadius.circular(24),
),
child: Text(
- _frames[_currentIndex].entry.year.toString(),
+ _frames[displayIndex].entry.year.toString(),
style: getEnteTextTheme(context).bodyMuted,
),
),
@@ -391,9 +371,6 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
Widget _buildFrameView(BuildContext context) {
final colorScheme = getEnteColorScheme(context);
- final isDark = Theme.of(context).brightness == Brightness.dark;
- final List<BoxShadow> cardShadow =
- isDark ? shadowFloatDark : shadowFloatLight;
if (_frames.isEmpty) {
return Center(
key: const ValueKey<String>("faces_timeline_empty"),
@@ -404,7 +381,9 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
decoration: BoxDecoration(
color: colorScheme.backgroundElevated,
borderRadius: BorderRadius.circular(28),
- boxShadow: cardShadow,
+ boxShadow: (Theme.of(context).brightness == Brightness.dark)
+ ? shadowFloatDark
+ : shadowFloatLight,
),
child: ClipRRect(
borderRadius: BorderRadius.circular(28),
@@ -423,40 +402,75 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
),
);
}
- final frame = _frames[_currentIndex];
- final key = ValueKey<int>(_frameViewVersion);
+ final stackProgress = _stackProgress.clamp(
+ 0.0,
+ (_frames.length - 1).toDouble(),
+ );
+ final isDark = Theme.of(context).brightness == Brightness.dark;
+ final List<_CardSlice> slices = [];
+ final startIndex = math.max(0, stackProgress.floor() - 3);
+ final endIndex = math.min(_frames.length - 1, stackProgress.ceil() + 4);
+
+ for (int i = startIndex; i <= endIndex; i++) {
+ final distance = i - stackProgress;
+ if (distance < -4.5 || distance > 5.5) {
+ continue;
+ }
+ slices.add(_CardSlice(index: i, distance: distance));
+ }
+
+ final futureSlices = slices.where((slice) => slice.distance >= 0).toList()
+ ..sort(
+ (a, b) => b.distance.compareTo(a.distance),
+ );
+ final presentAndPastSlices =
+ slices.where((slice) => slice.distance < 0).toList()
+ ..sort(
+ (a, b) => a.distance.compareTo(b.distance),
+ );
+
return Center(
- key: key,
child: FractionallySizedBox(
widthFactor: 0.82,
heightFactor: 0.78,
- child: DecoratedBox(
- decoration: BoxDecoration(
- color: colorScheme.backgroundElevated,
- borderRadius: BorderRadius.circular(28),
- boxShadow: cardShadow,
- ),
- child: ClipRRect(
- borderRadius: BorderRadius.circular(28),
- child: frame.image != null
- ? Image(
- image: frame.image!,
- fit: BoxFit.cover,
- width: double.infinity,
- height: double.infinity,
- gaplessPlayback: true,
- )
- : ColoredBox(
- color: colorScheme.backgroundElevated2,
- child: Center(
- child: Icon(
- Icons.person_outline,
- size: 72,
- color: colorScheme.strokeMuted,
- ),
+ child: LayoutBuilder(
+ builder: (context, constraints) {
+ final cardHeight = constraints.hasBoundedHeight
+ ? constraints.maxHeight
+ : constraints.biggest.height;
+ final orderedSlices = <_CardSlice>[
+ ...futureSlices,
+ ...presentAndPastSlices,
+ ];
+ final children = orderedSlices.isEmpty
+ ? [
+ _FacesTimelineCard(
+ key: ValueKey<int>(_currentIndex),
+ frame: _frames[_currentIndex],
+ distance: 0,
+ isDarkMode: isDark,
+ colorScheme: colorScheme,
+ cardHeight: cardHeight,
),
- ),
- ),
+ ]
+ : orderedSlices
+ .map(
+ (slice) => _FacesTimelineCard(
+ key: ValueKey<int>(slice.index),
+ frame: _frames[slice.index],
+ distance: slice.distance,
+ isDarkMode: isDark,
+ colorScheme: colorScheme,
+ cardHeight: cardHeight,
+ ),
+ )
+ .toList();
+ return Stack(
+ clipBehavior: Clip.none,
+ alignment: Alignment.center,
+ children: children,
+ );
+ },
),
),
);
@@ -541,22 +555,48 @@ class _FacesTimelinePageState extends State<FacesTimelinePage>
_startPlayback();
}
- _TransitionDirection _directionForIndex(int nextIndex) {
- if (_frames.length <= 1) {
- return _TransitionDirection.forward;
+ void _onCardAnimationTick() {
+ if (!_cardTransitionController.isAnimating && !_isAnimatingCard) {
+ return;
}
- if (_currentIndex == nextIndex) {
- return _transitionDirection;
+ final eased =
+ Curves.easeInOutCubic.transform(_cardTransitionController.value);
+ final progress = lerpDouble(
+ _animationStartProgress,
+ _targetIndex.toDouble(),
+ eased,
+ );
+ if (progress == null) {
+ return;
}
- if (_currentIndex == _frames.length - 1 && nextIndex == 0) {
- return _TransitionDirection.forward;
+ setState(() {
+ _stackProgress = progress;
+ });
+ }
+
+ void _onCardAnimationStatusChanged(AnimationStatus status) {
+ if (status != AnimationStatus.completed &&
+ status != AnimationStatus.dismissed) {
+ return;
}
- if (_currentIndex == 0 && nextIndex == _frames.length - 1) {
- return _TransitionDirection.backward;
+ if (_frames.isEmpty) {
+ setState(() {
+ _isAnimatingCard = false;
+ _stackProgress = 0;
+ });
+ return;
}
- return nextIndex > _currentIndex
- ? _TransitionDirection.forward
- : _TransitionDirection.backward;
+ final clampedIndex = _targetIndex.clamp(0, _frames.length - 1);
+ setState(() {
+ _isAnimatingCard = false;
+ _currentIndex = clampedIndex;
+ _stackProgress = clampedIndex.toDouble();
+ final frame = _frames[clampedIndex];
+ _previousCaptionValue = _currentCaptionValue;
+ _currentCaptionValue = frame.captionValue;
+ _currentCaptionType = frame.captionType;
+ _sliderValue = clampedIndex.toDouble();
+ });
}
}
@@ -576,7 +616,179 @@ class _TimelineFrame {
});
}
-enum _TransitionDirection { forward, backward }
+class _CardSlice {
+ final int index;
+ final double distance;
+
+ const _CardSlice({
+ required this.index,
+ required this.distance,
+ });
+}
+
+class _FacesTimelineCard extends StatelessWidget {
+ static const double _cardRadius = 28;
+
+ final _TimelineFrame frame;
+ final double distance;
+ final bool isDarkMode;
+ final EnteColorScheme colorScheme;
+ final double cardHeight;
+
+ const _FacesTimelineCard({
+ required this.frame,
+ required this.distance,
+ required this.isDarkMode,
+ required this.colorScheme,
+ required this.cardHeight,
+ super.key,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final scale = _calculateScale(distance);
+ final yOffset = _calculateYOffset(distance);
+ final opacity = _calculateOpacity(distance);
+ final blurSigma = _calculateBlur(distance);
+ final overlayOpacity =
+ distance > 0 ? math.min(0.45, 0.12 + distance * 0.12) : 0.0;
+
+ final cardShadow = _shadowForCard(distance);
+
+ final cardContent = ClipRRect(
+ borderRadius: BorderRadius.circular(_cardRadius),
+ child: Stack(
+ fit: StackFit.expand,
+ children: [
+ _buildImage(blurSigma),
+ if (overlayOpacity > 0)
+ Container(
+ color:
+ colorScheme.backgroundBase.withValues(alpha: overlayOpacity),
+ ),
+ if (frame.image == null)
+ Center(
+ child: Icon(
+ Icons.person_outline,
+ size: 72,
+ color: colorScheme.strokeMuted,
+ ),
+ ),
+ ],
+ ),
+ );
+
+ return Positioned.fill(
+ child: IgnorePointer(
+ child: Opacity(
+ opacity: opacity,
+ child: Transform.translate(
+ offset: Offset(0, yOffset),
+ child: Transform.scale(
+ scale: scale,
+ alignment: Alignment.center,
+ child: DecoratedBox(
+ decoration: BoxDecoration(
+ borderRadius: BorderRadius.circular(_cardRadius),
+ boxShadow: cardShadow,
+ ),
+ child: cardContent,
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+
+ Widget _buildImage(double blurSigma) {
+ final Widget base = frame.image != null
+ ? Image(
+ image: frame.image!,
+ fit: BoxFit.cover,
+ width: double.infinity,
+ height: double.infinity,
+ gaplessPlayback: true,
+ )
+ : ColoredBox(
+ color: colorScheme.backgroundElevated2,
+ );
+ if (blurSigma <= 0) {
+ return base;
+ }
+ return ImageFiltered(
+ imageFilter: ImageFilter.blur(
+ sigmaX: blurSigma,
+ sigmaY: blurSigma,
+ ),
+ child: base,
+ );
+ }
+
+ List<BoxShadow> _shadowForCard(double distance) {
+ final double baseOpacity = isDarkMode ? 0.55 : 0.3;
+ if (distance > 0) {
+ return [
+ BoxShadow(
+ color: Colors.black
+ .withValues(alpha: math.max(0.0, baseOpacity - distance * 0.12)),
+ blurRadius: 38,
+ offset: const Offset(0, 26),
+ spreadRadius: -6,
+ ),
+ ];
+ }
+ final dampening = math.max(0.2, 1 - distance.abs() * 0.25);
+ return [
+ BoxShadow(
+ color: Colors.black.withValues(alpha: baseOpacity * dampening),
+ blurRadius: 34,
+ offset: const Offset(0, 24),
+ spreadRadius: -12,
+ ),
+ ];
+ }
+
+ double _calculateScale(double distance) {
+ if (distance >= 0) {
+ return math.max(0.84, 1.0 - distance * 0.05);
+ }
+ final falling = 1.0 - distance.abs() * 0.02;
+ return falling.clamp(0.82, 1.02);
+ }
+
+ double _calculateYOffset(double distance) {
+ if (distance >= 0) {
+ final compression = math.pow(0.72, distance).toDouble();
+ return -cardHeight * 0.14 * distance * compression;
+ }
+ final downward = distance.abs();
+ final easedComponent = math.pow(downward, 1.45).toDouble();
+ final travel = downward * (2.8 + 1.8 * downward);
+ return cardHeight * (travel + easedComponent * 0.65);
+ }
+
+ double _calculateBlur(double distance) {
+ if (distance <= 0) {
+ return 0;
+ }
+ return math.min(20, (distance + 0.3) * 6);
+ }
+
+ double _calculateOpacity(double distance) {
+ if (distance >= 0) {
+ return math.max(0.35, 1 - distance * 0.22);
+ }
+ final drop = distance.abs();
+ const fadeStart = 0.9;
+ if (drop <= fadeStart) {
+ return 1.0;
+ }
+ const double fadeRange = 0.55;
+ final t = ((drop - fadeStart) / fadeRange).clamp(0.0, 1.0);
+ return math.max(0.0, 1.0 - t);
+ }
+}
enum _CaptionType { age, yearsAgo }