flutterdartanimationflutter-animation

Flutter: How to implement zoom animation while navigation


class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});

@override
State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
Future.delayed(Duration(seconds: 3), () {
  Navigator.of(context).push(
    MaterialPageRoute(
      builder: (context) => LoginScreen(),
    ),
  );
});
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
  body: Center(
    child: Image.asset("assets/logo.png"),
  ),
);
}
}

I have a Splash screen and after 3 seconds it will be navigated to Login screen. While navigating, I need to zoom the image and then the next screen will be appeared. How can I implement that. Anyone please help me to do that..


Solution

  • Try to replace _SplashScreenState with given below code

    class _SplashScreenState extends State<SplashScreen> with SingleTickerProviderStateMixin {
      late AnimationController _controller;
      late Animation<double> _animation;
    
      @override
      void initState() {
        super.initState();
    
        _controller = AnimationController(
          duration: const Duration(seconds: 2),
          vsync: this,
        );
    
        _animation = Tween<double>(begin: 0.0, end: 1.0).animate(
          CurvedAnimation(
            parent: _controller,
            curve: Curves.easeInOut,
          ),
        );
    
        _controller.forward();
    
        Future.delayed(Duration(seconds: 2), () {
          if (mounted) {
            Navigator.of(context).pushReplacement(
              ZoomPageRoute(
                page: LoginScreen(),
              ),
            );
          }
        });
      }
    
      @override
      void dispose() {
        _controller.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ScaleTransition(
              scale: _animation,
              child: Image.asset("assets/logo.png"),
            ),
          ),
        );
      }
    }
    

    for zoom effect use below code

    class ZoomPageRoute extends PageRouteBuilder {
      final Widget page;
    
      ZoomPageRoute({required this.page})
          : super(
              pageBuilder: (context, animation, secondaryAnimation) => page,
              transitionsBuilder: (context, animation, secondaryAnimation, child) {
                const begin = 0.0;
                const end = 1.0;
                const curve = Curves.easeInOut;
    
                var scaleTween = Tween<double>(begin: begin, end: end)
                    .chain(CurveTween(curve: curve));
    
                var scaleAnimation = animation.drive(scaleTween);
    
                return ScaleTransition(
                  scale: scaleAnimation,
                  child: child,
                );
              },
            );
    }