Skip to content

Commit

Permalink
fix: Infinite redraw in the gallery (#6355)
Browse files Browse the repository at this point in the history
  • Loading branch information
g123k authored Feb 11, 2025
1 parent 8681098 commit 740ea86
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 481 deletions.
94 changes: 10 additions & 84 deletions packages/smooth_app/lib/generic_lib/widgets/smooth_app_logo.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/themes/theme_provider.dart';

/// An animated logo which can depend on [SmoothSharedAnimationController]
Expand All @@ -28,11 +26,15 @@ class _SmoothAnimatedLogoState extends State<SmoothAnimatedLogo>
void initState() {
super.initState();

SchedulerBinding.instance.addPostFrameCallback((_) {
if (mounted) {
_attachAnimation();
}
});
_controller ??= AnimationController(
duration: const Duration(seconds: 1),
vsync: this,
)..addListener(_onAnimationChanged);

_animation = Tween<double>(begin: widget.opacityMin, end: widget.opacityMax)
.animate(_controller!);

_controller!.repeat(reverse: true);
}

@override
Expand All @@ -43,36 +45,14 @@ class _SmoothAnimatedLogoState extends State<SmoothAnimatedLogo>
);
}

void _attachAnimation() {
AnimationController? controller =
_SmoothSharedAnimationControllerState.of(context);

if (controller == null) {
_controller = AnimationController(
duration: const Duration(seconds: 1),
vsync: this,
);
controller = _controller;
}

_animation = Tween<double>(begin: widget.opacityMin, end: widget.opacityMax)
.animate(controller!)
..addListener(_onAnimationChanged);

if (!controller.isAnimating) {
controller.repeat(reverse: true);
}
}

void _onAnimationChanged() {
if (context.mounted) {
if (context.mounted && _controller!.isAnimating) {
setState(() {});
}
}

@override
void dispose() {
_animation?.removeListener(_onAnimationChanged);
_controller?.dispose();
super.dispose();
}
Expand All @@ -90,57 +70,3 @@ class SmoothAppLogo extends StatelessWidget {
);
}
}

/// A shared [AnimationController] that can be used by multiple
/// [SmoothAnimatedLogo] and ensure they are all perfectly synced.
class SmoothSharedAnimationController extends StatefulWidget {
const SmoothSharedAnimationController({
required this.child,
});

final Widget child;

@override
State<SmoothSharedAnimationController> createState() =>
_SmoothSharedAnimationControllerState();
}

class _SmoothSharedAnimationControllerState
extends State<SmoothSharedAnimationController>
with SingleTickerProviderStateMixin {
late AnimationController _controller;

@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 1),
vsync: this,
);
}

@override
Widget build(BuildContext context) {
return Provider<_SmoothSharedAnimationControllerState>(
create: (_) => this,
child: widget.child,
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}

static AnimationController? of(BuildContext context) {
try {
return Provider.of<_SmoothSharedAnimationControllerState>(
context,
listen: false,
)._controller;
} catch (_) {
return null;
}
}
}
Loading

0 comments on commit 740ea86

Please sign in to comment.