hero
" animated to the corresponding widget on another screen. During the animation, the widget is scaled up or down, and its position and size are interpolated to match the widget on the other screen.Here's an example of how to use a hero animation in Flutter:
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => SecondScreen()),
);
},
child: Hero(
tag: 'myHeroTag',
child: Image.asset('assets/my_image.png'),
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Center(
child: Hero(
tag: 'myHeroTag',
child: Image.asset('assets/my_image.png'),
),
),
),
);
}
}
In this example, the hero widget is an image that is displayed on both the first and second screen. The `GestureDetector
` widget is used to detect taps on the widget, and the `Navigator
` widget is used to navigate between the two screens. The `Hero
` widget is used to define the hero animation, and the tag is set to the same string on both screens. When the user taps on the image, the app navigates to the second screen, and the hero animation is automatically triggered, smoothly transitioning the image from the first to the second screen.