当前位置:   article > 正文

Flutter 之页面状态保持_flutter 保持页面状态

flutter 保持页面状态

一般情况下,我们使用tab切换的时候希望操作完毕之后,能够记住上个页面的状态,

但是使用Flutter的BottomNavigationBar的

时候默认是不记录页面状态的,即切换页面会导致重新加载。

这对我们来说很痛苦,而且非常的浪费资源

如果要想我们的页面在切换完毕之后记录之前的状态。需要一下几个步骤:

1、在包含BottomNavigationBar的页面中,body应该返回IndexedStack或者Pageview

  1. @override
  2. Widget build(BuildContext context) {
  3. return Scaffold(
  4. backgroundColor: mainBgColor,
  5. appBar: AppBar(
  6. elevation: 0.0,
  7. title: Text('Hello Flutter'),
  8. ),
  9. body: IndexedStack(
  10. index: _currentIndex,
  11. children: _tabBodies,
  12. ),
  13. bottomNavigationBar: BottomNavigationBar(
  14. currentIndex: _currentIndex,
  15. type: BottomNavigationBarType.fixed,
  16. backgroundColor: Colors.white,
  17. selectedItemColor: bottomTabSelectColor,
  18. items: _bottomTabs,
  19. onTap: setTabSelect,
  20. ),
  21. );
  22. }

2、想要保持状态的页面必须是StatefullWidget,并且在相应的页面的state中混入AutomaticKee

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号