11import 'package:flutter/material.dart' ;
2- import 'package:flutter/scheduler.dart' ;
32import 'package:flutter_svg/flutter_svg.dart' ;
4- import 'package:provider/provider.dart' ;
53import 'package:smooth_app/themes/theme_provider.dart' ;
64
75/// An animated logo which can depend on [SmoothSharedAnimationController]
@@ -28,11 +26,15 @@ class _SmoothAnimatedLogoState extends State<SmoothAnimatedLogo>
2826 void initState () {
2927 super .initState ();
3028
31- SchedulerBinding .instance.addPostFrameCallback ((_) {
32- if (mounted) {
33- _attachAnimation ();
34- }
35- });
29+ _controller ?? = AnimationController (
30+ duration: const Duration (seconds: 1 ),
31+ vsync: this ,
32+ )..addListener (_onAnimationChanged);
33+
34+ _animation = Tween <double >(begin: widget.opacityMin, end: widget.opacityMax)
35+ .animate (_controller! );
36+
37+ _controller! .repeat (reverse: true );
3638 }
3739
3840 @override
@@ -43,36 +45,14 @@ class _SmoothAnimatedLogoState extends State<SmoothAnimatedLogo>
4345 );
4446 }
4547
46- void _attachAnimation () {
47- AnimationController ? controller =
48- _SmoothSharedAnimationControllerState .of (context);
49-
50- if (controller == null ) {
51- _controller = AnimationController (
52- duration: const Duration (seconds: 1 ),
53- vsync: this ,
54- );
55- controller = _controller;
56- }
57-
58- _animation = Tween <double >(begin: widget.opacityMin, end: widget.opacityMax)
59- .animate (controller! )
60- ..addListener (_onAnimationChanged);
61-
62- if (! controller.isAnimating) {
63- controller.repeat (reverse: true );
64- }
65- }
66-
6748 void _onAnimationChanged () {
68- if (context.mounted) {
49+ if (context.mounted && _controller ! .isAnimating ) {
6950 setState (() {});
7051 }
7152 }
7253
7354 @override
7455 void dispose () {
75- _animation? .removeListener (_onAnimationChanged);
7656 _controller? .dispose ();
7757 super .dispose ();
7858 }
@@ -90,57 +70,3 @@ class SmoothAppLogo extends StatelessWidget {
9070 );
9171 }
9272}
93-
94- /// A shared [AnimationController] that can be used by multiple
95- /// [SmoothAnimatedLogo] and ensure they are all perfectly synced.
96- class SmoothSharedAnimationController extends StatefulWidget {
97- const SmoothSharedAnimationController ({
98- required this .child,
99- });
100-
101- final Widget child;
102-
103- @override
104- State <SmoothSharedAnimationController > createState () =>
105- _SmoothSharedAnimationControllerState ();
106- }
107-
108- class _SmoothSharedAnimationControllerState
109- extends State <SmoothSharedAnimationController >
110- with SingleTickerProviderStateMixin {
111- late AnimationController _controller;
112-
113- @override
114- void initState () {
115- super .initState ();
116- _controller = AnimationController (
117- duration: const Duration (seconds: 1 ),
118- vsync: this ,
119- );
120- }
121-
122- @override
123- Widget build (BuildContext context) {
124- return Provider <_SmoothSharedAnimationControllerState >(
125- create: (_) => this ,
126- child: widget.child,
127- );
128- }
129-
130- @override
131- void dispose () {
132- _controller.dispose ();
133- super .dispose ();
134- }
135-
136- static AnimationController ? of (BuildContext context) {
137- try {
138- return Provider .of <_SmoothSharedAnimationControllerState >(
139- context,
140- listen: false ,
141- )._controller;
142- } catch (_) {
143- return null ;
144- }
145- }
146- }
0 commit comments