flutter - 如何将 BottomNavigationBar 与 Navigator 一起使用

BottomNavigationBar 的 Flutter Gallery 示例在 Scaffold 的主体中使用 FadeTransitionsStack

我觉得如果我们可以使用 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/

相关文章:

dart - 如何在另一个 flutter 应用程序中使用本地 flutter 包?

flutter - 创建一个带有圆形边框的按钮

Flutter Layout Row/Column - 共享宽度,扩展高度

flutter - 如何确定屏幕高度和宽度

android-studio - 带有 Flutter 更新的 Android Studio 导致了

dart - Flutter - 从 Assets 中读取文本文件

dart - 使用不包含 MediaQuery 的上下文调用的 Flutter 错误 : Media

Flutter - 如何将用户数据传递给所有 View

flutter - 从 Flutter 中的 Scaffold AppBar 中删除阴影?

flutter - 弹出时强制 Flutter 导航器重新加载状态