Flutter BottomNavigationBarItem 标签切换时刷新 或 不刷新页面

页面:有3个底部标签 使用BottomNavigationBarItem实现

需求1:每次切换标签时需要刷新页面数据

需求2:进入页面时一次将3个页面的数据都加载完成

 

需求1 解决方法:

body 使用:_children[页面序号]   

例:body: _children[widget.currentIndex],

class Home extends StatefulWidget {
  int currentIndex;

  Home({Key key, this.currentIndex = 0}) : super(key:key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

  final List<Widget> _children = [Home1(), Home2(), Home3()];
 final List<BottomNavigationBarItem> _list = <BottomNavigationBarItem>[
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text('标签1', style: TextStyle(fontSize: 14),),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text('标签2', style: TextStyle(fontSize: 14),),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text('标签3', style: TextStyle(fontSize: 14),),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text('标签4', style: TextStyle(fontSize: 14),),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      bottomNavigationBar:BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        onTap: onTabTapped,
        currentIndex: widget.currentIndex,
        items: _list,
      ),
        //重点
      body: _children[widget.currentIndex],
    );
  }

  void onTabTapped(int index) {
    setState(() {
      widget.currentIndex = index;
    });
  }

}

需求2 :解决方法

body使用: IndexedStack(index: 页面序号, children: 页面列表,),

例: body: IndexedStack(
             index: widget.currentIndex,
             children: _children,
        ),

class Home extends StatefulWidget {
  int currentIndex;

  Home({Key key, this.currentIndex = 0}) : super(key:key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

  final List<Widget> _children = [Home1(), Home2(), Home3()];
 final List<BottomNavigationBarItem> _list = <BottomNavigationBarItem>[
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text('标签1', style: TextStyle(fontSize: 14),),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text('标签2', style: TextStyle(fontSize: 14),),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text('标签3', style: TextStyle(fontSize: 14),),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text('标签4', style: TextStyle(fontSize: 14),),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      bottomNavigationBar:BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        onTap: onTabTapped,
        currentIndex: widget.currentIndex,
        items: _list,
      ),
        //重点
      body: IndexedStack(
        index: widget.currentIndex,
        children: _children,
      ),
    );
  }

  void onTabTapped(int index) {
    setState(() {
      widget.currentIndex = index;
    });
  }

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值