free geoip
2

Hero Animation in Flutter App Guide

When building modern mobile applications, animations play a key role in improving user experience. In Flutter, one of the most…

When building modern mobile applications, animations play a key role in improving user experience. In Flutter, one of the most powerful and commonly used animations is the Hero Animation. This effect allows seamless transitions between screens by animating shared widgets, giving users a smooth and visually appealing interaction. In this guide, we will explore how to implement Hero Animation in Flutter App, complete with detailed explanations and working examples.

Hero Animation in Flutter App

What is Hero Animation in Flutter?

Hero Animation is a built-in Flutter widget that enables beautiful transitions between two screens. The widget is named Hero, and it works by tagging a widget on one screen and transitioning it to another screen where the same tag exists. This creates the illusion that the widget “flies” from one screen to another, enhancing the navigation experience.

Why Use Hero Animation?

  • Enhances the overall look and feel of your Flutter app.
  • Provides smooth screen transitions.
  • Improves user engagement by making navigation visually appealing.
  • Simple to implement using Flutter’s built-in Hero widget.

Basic Syntax of Hero Widget

Hero(
  tag: 'uniqueTag',
  child: YourWidget(),
)

The tag must be unique and the same across both screens to enable the animation. The child is the widget you want to animate.

Example: Implementing Hero Animation in Flutter

Let’s create a simple app that demonstrates Hero Animation between a list of images and a detailed view screen.

Step 1: Create the Main Screen

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Hero Animation Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Hero Animation Example')),
      body: GridView.builder(
        padding: const EdgeInsets.all(10),
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2, 
          crossAxisSpacing: 10, 
          mainAxisSpacing: 10,
        ),
        itemCount: 6,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => DetailPage(imageIndex: index),
                ),
              );
            },
            child: Hero(
              tag: 'image$index',
              child: Image.asset(
                'assets/image${index + 1}.jpg',
                fit: BoxFit.cover,
              ),
            ),
          );
        },
      ),
    );
  }
}

Step 2: Create the Detail Screen

class DetailPage extends StatelessWidget {
  final int imageIndex;

  const DetailPage({super.key, required this.imageIndex});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Detail Page')),
      body: Center(
        child: Hero(
          tag: 'image$imageIndex',
          child: Image.asset(
            'assets/image${imageIndex + 1}.jpg',
            fit: BoxFit.contain,
          ),
        ),
      ),
    );
  }
}

How This Works

  1. Each image in the grid is wrapped with a Hero widget, and given a unique tag.
  2. When tapped, the app navigates to the DetailPage, which also contains a Hero widget with the same tag.
  3. Flutter automatically animates the image transition, creating a smooth “hero” effect.

Best Practices for Hero Animation

  • Always use unique tags for different widgets.
  • Ensure both Hero widgets (source and destination) have the same tag.
  • Hero animations work best with images, icons, and other visual widgets.
  • Combine with PageRouteBuilder for more customized transitions.

Advanced Hero Animation

Hero animations can be combined with other animations for more complex effects. For example, you can wrap a Hero widget with AnimatedContainer or use PageRouteBuilder to add fade and slide effects along with Hero transitions.

Example with Fade Transition

Navigator.push(
  context,
  PageRouteBuilder(
    pageBuilder: (context, animation, secondaryAnimation) => DetailPage(imageIndex: index),
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      return FadeTransition(opacity: animation, child: child);
    },
  ),
);

Conclusion

The Hero Animation in Flutter App is a simple yet powerful tool to create visually engaging transitions between screens. By using the Hero widget, you can enhance your app’s navigation and improve user satisfaction. Whether it’s for images, icons, or other UI elements, hero animations make apps look polished and professional.

For further reading, you can explore the official Flutter documentation on Hero animations at Flutter Hero Animation Docs.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *