如何将BottomNavigationBar 与Navigator 一起使用? [英] How to use BottomNavigationBar with Navigator?

查看:43
本文介绍了如何将BottomNavigationBar 与Navigator 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The Flutter Gallery example of BottomNavigationBar uses a Stack of FadeTransitions in the body of the Scaffold.

我觉得如果我们可以使用 Navigator 来切换页面会更干净(也更容易制作动画).

I feel it would be cleaner (and easier to animate) if we could switch pages by using a 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.

You should keep each page by Stack to keep their state. Offstage stops painting, TickerMode stops animation. MaterialApp includes Navigator.

这篇关于如何将BottomNavigationBar 与Navigator 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆