immich/mobile/lib/widgets/common/mesmerizing_sliver_app_bar.dart

595 lines
17 KiB
Dart
Raw Normal View History

2025-07-08 15:31:31 -05:00
import 'dart:io';
2025-07-07 22:25:14 -05:00
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
2025-07-08 12:02:33 -05:00
import 'package:immich_mobile/domain/services/timeline.service.dart';
2025-07-07 22:25:14 -05:00
import 'package:immich_mobile/extensions/build_context_extensions.dart';
2025-07-08 12:02:33 -05:00
import 'package:immich_mobile/extensions/translate_extensions.dart';
2025-07-07 22:25:14 -05:00
import 'package:immich_mobile/presentation/widgets/images/image_provider.dart';
import 'package:immich_mobile/providers/infrastructure/timeline.provider.dart';
2025-07-08 12:02:33 -05:00
import 'package:immich_mobile/providers/timeline/multiselect.provider.dart';
2025-07-07 22:25:14 -05:00
2025-07-08 15:31:31 -05:00
class MesmerizingSliverAppBar extends ConsumerStatefulWidget {
2025-07-08 12:02:33 -05:00
const MesmerizingSliverAppBar({
super.key,
required this.title,
2025-07-08 12:58:38 -05:00
this.icon = Icons.camera,
2025-07-08 12:02:33 -05:00
});
final String title;
2025-07-08 12:58:38 -05:00
final IconData icon;
2025-07-08 12:02:33 -05:00
2025-07-08 15:31:31 -05:00
@override
ConsumerState<MesmerizingSliverAppBar> createState() =>
_MesmerizingSliverAppBarState();
}
class _MesmerizingSliverAppBarState
extends ConsumerState<MesmerizingSliverAppBar> {
double _scrollProgress = 0.0;
2025-07-08 12:02:33 -05:00
double _calculateScrollProgress(FlexibleSpaceBarSettings? settings) {
if (settings?.maxExtent == null || settings?.minExtent == null) {
return 1.0;
}
final deltaExtent = settings!.maxExtent - settings.minExtent;
if (deltaExtent <= 0.0) {
return 1.0;
}
return (1.0 - (settings.currentExtent - settings.minExtent) / deltaExtent)
.clamp(0.0, 1.0);
}
2025-07-07 22:25:14 -05:00
@override
2025-07-08 15:31:31 -05:00
Widget build(BuildContext context) {
2025-07-07 22:25:14 -05:00
final timelineService = ref.watch(timelineServiceProvider);
final assetCount = timelineService.totalAssets;
2025-07-08 12:02:33 -05:00
final isMultiSelectEnabled =
ref.watch(multiSelectProvider.select((s) => s.isEnabled));
2025-07-08 15:31:31 -05:00
2025-07-09 10:20:06 -05:00
return isMultiSelectEnabled
? const SliverToBoxAdapter(child: SizedBox())
: SliverAppBar(
expandedHeight: 300.0,
floating: false,
pinned: true,
snap: false,
elevation: 0,
leading: IconButton(
icon: Icon(
Platform.isIOS
? Icons.arrow_back_ios_new_rounded
: Icons.arrow_back,
color: Color.lerp(
Colors.white,
context.primaryColor,
_scrollProgress,
),
shadows: [
_scrollProgress < 0.95
? Shadow(
offset: const Offset(0, 2),
blurRadius: 5,
color: Colors.black.withValues(alpha: 0.5),
)
: const Shadow(
offset: Offset(0, 2),
blurRadius: 0,
color: Colors.transparent,
),
],
),
onPressed: () {
context.pop();
},
2025-07-08 15:31:31 -05:00
),
2025-07-09 10:20:06 -05:00
flexibleSpace: Builder(
builder: (context) {
final settings = context.dependOnInheritedWidgetOfExactType<
FlexibleSpaceBarSettings>();
final scrollProgress = _calculateScrollProgress(settings);
2025-07-08 12:02:33 -05:00
2025-07-09 10:20:06 -05:00
// Update scroll progress for the leading button
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted && _scrollProgress != scrollProgress) {
setState(() {
_scrollProgress = scrollProgress;
});
}
2025-07-08 15:31:31 -05:00
});
2025-07-09 10:20:06 -05:00
return FlexibleSpaceBar(
centerTitle: true,
title: AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
child: scrollProgress > 0.95
? Text(
widget.title,
style: TextStyle(
color: context.primaryColor,
fontWeight: FontWeight.w600,
fontSize: 18,
),
)
: null,
),
background: _ExpandedBackground(
assetCount: assetCount,
scrollProgress: scrollProgress,
title: widget.title,
icon: widget.icon,
),
);
},
),
);
2025-07-07 22:25:14 -05:00
}
}
2025-07-08 15:31:31 -05:00
class _ExpandedBackground extends ConsumerStatefulWidget {
2025-07-07 22:25:14 -05:00
final int assetCount;
final double scrollProgress;
2025-07-08 12:02:33 -05:00
final String title;
2025-07-08 12:58:38 -05:00
final IconData icon;
2025-07-07 22:25:14 -05:00
const _ExpandedBackground({
required this.assetCount,
required this.scrollProgress,
2025-07-08 12:02:33 -05:00
required this.title,
2025-07-08 12:58:38 -05:00
required this.icon,
2025-07-07 22:25:14 -05:00
});
@override
2025-07-08 15:31:31 -05:00
ConsumerState<_ExpandedBackground> createState() =>
_ExpandedBackgroundState();
}
class _ExpandedBackgroundState extends ConsumerState<_ExpandedBackground>
with SingleTickerProviderStateMixin {
late AnimationController _slideController;
late Animation<Offset> _slideAnimation;
@override
void initState() {
super.initState();
_slideController = AnimationController(
duration: const Duration(milliseconds: 800),
vsync: this,
);
_slideAnimation = Tween<Offset>(
begin: const Offset(0, 1.5),
end: Offset.zero,
).animate(
CurvedAnimation(
parent: _slideController,
curve: Curves.easeOutCubic,
),
);
Future.delayed(const Duration(milliseconds: 100), () {
if (mounted) {
_slideController.forward();
}
});
}
@override
void dispose() {
_slideController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
2025-07-07 22:25:14 -05:00
final timelineService = ref.watch(timelineServiceProvider);
return Stack(
fit: StackFit.expand,
children: [
Transform.translate(
2025-07-08 15:31:31 -05:00
offset: Offset(0, widget.scrollProgress * 50),
2025-07-07 22:25:14 -05:00
child: Transform.scale(
2025-07-08 15:31:31 -05:00
scale: 1.4 - (widget.scrollProgress * 0.2),
2025-07-08 12:58:38 -05:00
child: _RandomAssetBackground(
timelineService: timelineService,
2025-07-08 15:31:31 -05:00
icon: widget.icon,
2025-07-08 12:58:38 -05:00
),
2025-07-07 22:25:14 -05:00
),
),
2025-07-08 12:02:33 -05:00
Container(
2025-07-07 22:25:14 -05:00
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
2025-07-08 12:02:33 -05:00
colors: [
Colors.transparent,
Colors.transparent,
Colors.black.withValues(
2025-07-08 15:31:31 -05:00
alpha: 0.6 + (widget.scrollProgress * 0.2),
2025-07-08 12:02:33 -05:00
),
],
2025-07-08 15:31:31 -05:00
stops: const [0.0, 0.65, 1.0],
2025-07-07 22:25:14 -05:00
),
),
),
Positioned(
bottom: 16,
left: 16,
2025-07-08 15:31:31 -05:00
child: SlideTransition(
position: _slideAnimation,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
widget.title,
style: const TextStyle(
2025-07-08 12:02:33 -05:00
color: Colors.white,
2025-07-08 15:31:31 -05:00
fontSize: 36,
fontWeight: FontWeight.bold,
letterSpacing: 0.5,
2025-07-08 12:02:33 -05:00
shadows: [
2025-07-08 15:31:31 -05:00
Shadow(
offset: Offset(0, 2),
blurRadius: 12,
2025-07-08 12:02:33 -05:00
color: Colors.black45,
2025-07-07 22:25:14 -05:00
),
2025-07-08 12:02:33 -05:00
],
2025-07-07 22:25:14 -05:00
),
2025-07-08 12:02:33 -05:00
),
2025-07-08 15:31:31 -05:00
AnimatedContainer(
duration: const Duration(milliseconds: 300),
child: Text(
'items_count'.t(
context: context,
args: {"count": widget.assetCount},
),
style: context.textTheme.labelLarge?.copyWith(
// letterSpacing: 0.2,
fontWeight: FontWeight.bold,
color: Colors.white,
shadows: [
const Shadow(
offset: Offset(0, 1),
blurRadius: 6,
color: Colors.black45,
),
],
),
),
),
],
),
2025-07-07 22:25:14 -05:00
),
),
],
);
}
}
class _RandomAssetBackground extends StatefulWidget {
2025-07-08 12:02:33 -05:00
final TimelineService timelineService;
2025-07-08 12:58:38 -05:00
final IconData icon;
2025-07-07 22:25:14 -05:00
2025-07-08 12:58:38 -05:00
const _RandomAssetBackground({
required this.timelineService,
required this.icon,
});
2025-07-07 22:25:14 -05:00
@override
State<_RandomAssetBackground> createState() => _RandomAssetBackgroundState();
}
class _RandomAssetBackgroundState extends State<_RandomAssetBackground>
with TickerProviderStateMixin {
late AnimationController _zoomController;
2025-07-08 15:31:31 -05:00
late AnimationController _crossFadeController;
2025-07-07 22:25:14 -05:00
late Animation<double> _zoomAnimation;
late Animation<Offset> _panAnimation;
2025-07-08 15:31:31 -05:00
late Animation<double> _crossFadeAnimation;
2025-07-07 22:25:14 -05:00
BaseAsset? _currentAsset;
BaseAsset? _nextAsset;
2025-07-08 15:31:31 -05:00
bool _isZoomingIn = true;
2025-07-07 22:25:14 -05:00
2025-07-08 12:58:38 -05:00
final LinearGradient gradient = LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Colors.pink.shade300.withValues(alpha: 0.9),
Colors.purple.shade400.withValues(alpha: 0.8),
Colors.indigo.shade400.withValues(alpha: 0.9),
Colors.blue.shade500.withValues(alpha: 0.8),
],
stops: const [0.0, 0.3, 0.7, 1.0],
);
2025-07-07 22:25:14 -05:00
@override
void initState() {
super.initState();
_zoomController = AnimationController(
2025-07-08 15:31:31 -05:00
duration: const Duration(seconds: 12),
2025-07-07 22:25:14 -05:00
vsync: this,
);
2025-07-08 15:31:31 -05:00
_crossFadeController = AnimationController(
duration: const Duration(milliseconds: 1200),
2025-07-07 22:25:14 -05:00
vsync: this,
);
_zoomAnimation = Tween<double>(
2025-07-08 12:02:33 -05:00
begin: 1.0,
2025-07-08 15:31:31 -05:00
end: 1.2,
2025-07-07 22:25:14 -05:00
).animate(
CurvedAnimation(
parent: _zoomController,
curve: Curves.easeInOut,
),
);
_panAnimation = Tween<Offset>(
2025-07-08 12:02:33 -05:00
begin: Offset.zero,
2025-07-08 15:31:31 -05:00
end: const Offset(0.15, -0.5),
2025-07-07 22:25:14 -05:00
).animate(
CurvedAnimation(
parent: _zoomController,
curve: Curves.easeInOut,
),
);
2025-07-08 15:31:31 -05:00
_crossFadeAnimation = Tween<double>(
2025-07-07 22:25:14 -05:00
begin: 0.0,
end: 1.0,
).animate(
CurvedAnimation(
2025-07-08 15:31:31 -05:00
parent: _crossFadeController,
curve: Curves.easeInOutCubic,
2025-07-07 22:25:14 -05:00
),
);
2025-07-08 12:02:33 -05:00
Future.delayed(
2025-07-08 12:58:38 -05:00
Durations.medium1,
2025-07-08 15:31:31 -05:00
() => _loadFirstAsset(),
2025-07-08 12:02:33 -05:00
);
2025-07-07 22:25:14 -05:00
}
@override
void dispose() {
_zoomController.dispose();
2025-07-08 15:31:31 -05:00
_crossFadeController.dispose();
2025-07-07 22:25:14 -05:00
super.dispose();
}
2025-07-08 15:31:31 -05:00
void _startAnimationCycle() {
if (_isZoomingIn) {
_zoomController.forward().then((_) {
2025-07-07 22:25:14 -05:00
_loadNextAsset();
2025-07-08 15:31:31 -05:00
});
} else {
_zoomController.reverse().then((_) {
_loadNextAsset();
});
}
2025-07-07 22:25:14 -05:00
}
2025-07-08 15:31:31 -05:00
Future<void> _loadFirstAsset() async {
2025-07-08 12:02:33 -05:00
if (!mounted) {
return;
}
2025-07-07 22:25:14 -05:00
2025-07-08 12:02:33 -05:00
if (widget.timelineService.totalAssets == 0) {
setState(() {
_currentAsset = null;
});
return;
2025-07-07 22:25:14 -05:00
}
2025-07-08 12:02:33 -05:00
setState(() {
2025-07-08 14:56:01 -05:00
_currentAsset = widget.timelineService.getRandomAsset();
2025-07-08 12:02:33 -05:00
});
2025-07-08 15:31:31 -05:00
await _crossFadeController.forward();
2025-07-08 12:02:33 -05:00
if (_zoomController.status == AnimationStatus.dismissed) {
2025-07-08 15:31:31 -05:00
if (_isZoomingIn) {
_zoomController.reset();
} else {
_zoomController.value = 1.0;
}
_startAnimationCycle();
2025-07-07 22:25:14 -05:00
}
}
Future<void> _loadNextAsset() async {
2025-07-08 12:02:33 -05:00
if (!mounted) {
return;
}
2025-07-07 22:25:14 -05:00
try {
if (widget.timelineService.totalAssets > 1) {
2025-07-08 15:31:31 -05:00
// Load next asset while keeping current one visible
final nextAsset = widget.timelineService.getRandomAsset();
2025-07-08 14:56:01 -05:00
setState(() {
2025-07-08 15:31:31 -05:00
_nextAsset = nextAsset;
2025-07-08 14:56:01 -05:00
});
2025-07-08 15:31:31 -05:00
await _crossFadeController.reverse();
2025-07-08 14:56:01 -05:00
setState(() {
_currentAsset = _nextAsset;
_nextAsset = null;
});
2025-07-08 15:31:31 -05:00
_crossFadeController.value = 1.0;
_isZoomingIn = !_isZoomingIn;
_startAnimationCycle();
2025-07-07 22:25:14 -05:00
}
2025-07-08 12:02:33 -05:00
} catch (e) {
_zoomController.reset();
2025-07-08 15:31:31 -05:00
_startAnimationCycle();
2025-07-07 22:25:14 -05:00
}
}
@override
Widget build(BuildContext context) {
if (widget.timelineService.totalAssets == 0) {
2025-07-08 12:58:38 -05:00
return _EmptyPageExtendedBackground(
gradient: gradient,
icon: widget.icon,
2025-07-07 22:25:14 -05:00
);
}
if (_currentAsset == null) {
2025-07-08 12:58:38 -05:00
return const SizedBox.shrink();
2025-07-07 22:25:14 -05:00
}
return AnimatedBuilder(
2025-07-08 15:31:31 -05:00
animation: Listenable.merge(
[_zoomAnimation, _panAnimation, _crossFadeAnimation],
),
2025-07-07 22:25:14 -05:00
builder: (context, child) {
2025-07-08 14:56:01 -05:00
return Transform.scale(
scale: _zoomAnimation.value,
2025-07-08 15:31:31 -05:00
child: Stack(
fit: StackFit.expand,
children: [
// Current image
if (_currentAsset != null)
Opacity(
opacity: _crossFadeAnimation.value,
child: SizedBox(
width: double.infinity,
height: double.infinity,
child: Image(
alignment: Alignment.topRight,
image: getFullImageProvider(_currentAsset!),
fit: BoxFit.cover,
frameBuilder:
(context, child, frame, wasSynchronouslyLoaded) {
if (wasSynchronouslyLoaded || frame != null) {
return child;
}
return Container();
},
errorBuilder: (context, error, stackTrace) {
return Container(
decoration: BoxDecoration(gradient: gradient),
);
},
),
),
),
// Next image (for cross-fade)
if (_nextAsset != null)
Opacity(
opacity: 1.0 - _crossFadeAnimation.value,
child: SizedBox(
width: double.infinity,
height: double.infinity,
child: Image(
alignment: Alignment.topRight,
image: getFullImageProvider(_nextAsset!),
fit: BoxFit.cover,
frameBuilder:
(context, child, frame, wasSynchronouslyLoaded) {
if (wasSynchronouslyLoaded || frame != null) {
return child;
}
return Container();
},
errorBuilder: (context, error, stackTrace) {
return Container(
decoration: BoxDecoration(gradient: gradient),
);
},
),
),
),
],
2025-07-07 22:25:14 -05:00
),
);
},
);
}
}
2025-07-08 12:58:38 -05:00
class _EmptyPageExtendedBackground extends StatelessWidget {
const _EmptyPageExtendedBackground({
required this.gradient,
required this.icon,
});
final LinearGradient gradient;
final IconData icon;
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(gradient: gradient),
child: Stack(
children: [
Positioned(
top: 40,
right: 30,
child: Container(
width: 80,
height: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: context.isDarkTheme
? Colors.white.withValues(alpha: 0.1)
: Colors.white.withValues(alpha: 0.2),
),
),
),
Positioned(
bottom: 100,
left: 50,
child: Container(
width: 60,
height: 60,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: context.isDarkTheme
? Colors.white.withValues(alpha: 0.08)
: Colors.white.withValues(alpha: 0.15),
),
),
),
Positioned(
top: 120,
left: 20,
child: Container(
width: 40,
height: 40,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: context.isDarkTheme
? Colors.white.withValues(alpha: 0.06)
: Colors.white.withValues(alpha: 0.12),
),
),
),
Center(
child: Icon(
icon,
size: 100,
color: context.isDarkTheme
? Colors.white.withValues(alpha: 0.15)
: Colors.white.withValues(alpha: 0.25),
),
),
],
),
);
}
}