How to use BottomNavigationBar with Navigator?

后端 未结 6 777
北荒
北荒 2020-12-02 08:36

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

I

6条回答
  •  無奈伤痛
    2020-12-02 08:56

    int index = 0;
    
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        body: new Stack(
          children: [
            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: [
            new BottomNavigationBarItem(
              icon: new Icon(Icons.home),
              title: new Text("Left"),
            ),
            new BottomNavigationBarItem(
              icon: new Icon(Icons.search),
              title: new Text("Right"),
            ),
          ],
        ),
      );
    }
    

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

提交回复
热议问题