Let's say I have two pages, Page1 and Page2 containing a Hero. In Page2, I want to start a Widget animation after the Hero animation has finished.
Is it possible to get notified in Page2 via a Callback about the state of the Hero animation?
The only workaround I found so far is to add a delay to the Widget animation to avoid that it starts before the Hero animation has finished:
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) => Container(
child: Hero(
tag: "hero-tag",
child: IconButton(
icon: Icon(Icons.person),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => Page2(),
));
}),
),
);
}
class Page2 extends StatefulWidget {
@override
_Page2State createState() => _Page2State();
}
class _Page2State extends State<Page2> with TickerProviderStateMixin {
AnimationController _controller;
Animation _fabAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 400), vsync: this);
_fabAnimation = Tween<double>(
begin: 0.0,
end: 1.0,
).animate(
CurvedAnimation(
parent: _controller,
// delay to wait for hero animation to end
curve: Interval(
0.300,
1.000,
curve: Curves.ease,
),
),
);
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
child: Row(
children: <Widget>[
Hero(
tag: "hero-tag",
child: Icon(Icons.person),
),
ScaleTransition(
scale: _fabAnimation,
child: FloatingActionButton(
child: Icon(
Icons.camera_alt,
),
onPressed: () {},
),
),
],
),
);
}
}