虽然目前没有内置方法可以在任何特定方向禁用英雄动画,但 CLucera 使用 FadeTransition 和 HeroFlightDirection 是一种创造性的方式,最直接的方法是打破 tag两个英雄之间的关联:
当你从 2 号英雄回到 1 号英雄时,只是暂时将 1 号英雄的标签更改为其他东西,然后英雄不会动画回来。一个简化的例子:
class _MyHomePageState extends State<MyHomePage> {
String tag1, tag2;
String sharedTag = 'test';
String breakTag = 'notTest';
@override
void initState() {
super.initState();
tag1 = sharedTag;
tag2 = sharedTag;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Hero(
tag: tag1,
child: RaisedButton(
child: Text("hi"),
onPressed: () {
// restore the tag
if (tag1 != sharedTag) {
setState(() {
tag1 = sharedTag;
});
}
// second route
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
alignment: Alignment.topLeft,
child: Hero(
tag: tag2,
child: RaisedButton(
child: Text('hello'),
onPressed: () {
// change the tag to disable the reverse anim
setState(() {
tag1 = breakTag;
});
Navigator.of(context).pop();
},
),
)
),
);
}
)
);
},
)
),
),
);
}
}
但是如果你想直接修改动画,那么在flightShuttleBuilder 里面玩是像 CLucera 那样做的方法。您还可以查看medium/mastering-hero-animations-in-flutter 以进一步探索该区域。