我认为正确的做法是在两种情况下都使用相同标记包装BottomNavigationBar,以Hero的形式呈现。这样,在页面之间的动画发生时,它们将被排除。
这只是一个简短的示例,但我强烈建议进行清理,即传递hero字符串,使用小部件而不是大块的构建,为BottomNavigationBar创建自己的小部件。
请注意,在hero转换期间,在我的手机上它会溢出0.0000191像素
,但在发布模式下,我认为这不应该是问题。
import 'package:flutter/material.dart';
void main() => runApp(new MaterialApp(
home: new Builder(
builder: (context) => new Scaffold(
bottomNavigationBar: new Hero(
tag: "bottomNavigationBar",
child: new BottomNavigationBar(items: [
new BottomNavigationBarItem(icon: new Icon(Icons.home), title: new Text("Home")),
new BottomNavigationBarItem(icon: new Icon(Icons.ac_unit), title: new Text("AC Unit"))
]),
),
body: new SafeArea(
child: new Container(
constraints: new BoxConstraints.expand(),
color: Colors.green,
child: new Column(
children: <Widget>[
new RaisedButton(
child: new Text("Press me"),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new Scaffold(
bottomNavigationBar: new Hero(
tag: "bottomNavigationBar",
child: new BottomNavigationBar(items: [
new BottomNavigationBarItem(icon: new Icon(Icons.home), title: new Text("Home")),
new BottomNavigationBarItem(icon: new Icon(Icons.ac_unit), title: new Text("AC Unit"))
]),
),
body: new SafeArea(
child: new Container(
constraints:
new BoxConstraints.expand(),
color: Colors.red,
child: new Column(
children: <Widget>[
new RaisedButton(
onPressed: () =>
Navigator.pop(context),
child: new Text("Back"),
)
],
),
),
),
)));
})
],
),
),
),
),
),
));
我不知道
hero
系统如何处理多个英雄等情况,如果您想要动画导航栏,可能效果不佳。
还有另一种方法可以允许您动画底部导航栏;实际上,这已经是一个被回答过的问题了:
Flutter: Hero transition + widget animation at the same time?
button
是不是在BottomNavigationBar
里面? - Rémi RousseletTweetViewController
就会被推出,底部栏仍然可见。我认为几乎所有流行的应用程序都具有这种行为。 - harry