flutter 首页导航切换 不重载 不刷新 不销毁widget 原

假如用了Navigator 跳出当前页面,就会发生dispose事件,所以要想保留住页面,就必须wiget加载在当前页面

使用到的方法

Scaffold 叠加Widget 第二个使用Offstage隐藏 其他窗口  导航栏切换的时候 只显示一个

首屏创建只包含底部导航栏widget  然后widget是头部和body即可。

 

  Widget build(BuildContext context) {

    return new MaterialApp(
        title: '123',
        theme: new ThemeData(
            primaryColor: Colors.white
        ),
        home: Scaffold(
          body: Stack(
            children: <Widget>[
              new Offstage(
                offstage: _selectedIndex!=0, //这里控制
                child: WeiboWidget(),
              ),
              new Offstage(
                offstage: _selectedIndex!=1, //这里控制
                child: OrderWidget(),
              ),
              new Offstage(
                offstage: _selectedIndex!=2, //这里控制
                child: ServerWidget(),
              ),
              new Offstage(
                offstage: _selectedIndex!=3, //这里控制
                child: MpWidget(),
              ),
              new Offstage(
                offstage: _selectedIndex!=4, //这里控制
                child: MpWidget(),
              ),
            ],
          ),
          bottomNavigationBar: MyBottom(),
        )
    );
  }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章