BottomNavigationBar
的 Flutter Gallery 示例在 Scaffold
的主体中使用 FadeTransitions
的 Stack
。
我觉得如果我们可以使用 Navigator
切换页面会更简洁(也更容易制作动画)。
有这方面的例子吗?
最佳答案
int index = 0;
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(
children: <Widget>[
new Offstage(
offstage: index != 0,
child: new TickerMode(
enabled: index == 0,
child: new MaterialApp(home: new YourLeftPage()),
),
),
new Offstage(
offstage: index != 1,
child: new TickerMode(
enabled: index == 1,
child: new MaterialApp(home: new YourRightPage()),
),
),
],
),
bottomNavigationBar: new BottomNavigationBar(
currentIndex: index,
onTap: (int index) { setState((){ this.index = index; }); },
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: new Text("Left"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.search),
title: new Text("Right"),
),
],
),
);
}
您应该通过 Stack
保留每个页面以保持其状态。
Offstage
停止绘画,TickerMode
停止动画。
MaterialApp
包括 Navigator
。
关于flutter - 如何将 BottomNavigationBar 与 Navigator 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45235570/