问题描述

BottomNavigationBar 是flutter 中最常用的UI组建,刚接触时发现在切换tab 的时候,会刷新当前的所有状态,每个页面都会重新刷新。于是乎,在这里先记录下解决方案。

BottomNavigationBar基本代码

以下Home 是首页,切入三个tab,代码如下:


class Home extends StatefulWidget {
@override
createState() => _Home();
} class _Home extends State<Home> {
int _currentIndex = 0;
List<Widget> _pages; @override
void initState() {
_pages = [
new Guild(),
new ActivityList(),
new Mine(),
];
super.initState(); } @override
void dispose() {
super.dispose();
_pageController.dispose();
} var _pageController = PageController(); @override
Widget build(BuildContext context) {
return Scaffold(
body: PageView.builder(
controller: _pageController,
physics: NeverScrollableScrollPhysics(),
onPageChanged: _pageChanged,
itemCount: _pages.length,
itemBuilder: (context, index) => _pages[index]),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
fixedColor: Colors.black,//点击选择
type: BottomNavigationBarType.shifting,
onTap: onTabTapped,
items: [
BottomNavigationBarItem(
//backgroundColor: Theme.of(context).appBarTheme.color
icon: Icon(Icons.home),
title: Text("公会"),
backgroundColor: Theme.of(context).appBarTheme.color),
BottomNavigationBarItem(
icon: Icon(Icons.notifications),
title: Text("活动"),
backgroundColor: Theme.of(context).appBarTheme.color),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text("我的"),
backgroundColor: Theme.of(context).appBarTheme.color),
],
),
);
} void _pageChanged(int index) {
setState(() {
if (_currentIndex != index) _currentIndex = index;
});
} void onTabTapped(int index) {
_pageController.jumpToPage(index);
}
}
 

这样像简单的写,其实就会出现 切换tab 的时候,重新刷新当前页面的现象,之前保留的状态就会消失。

解决方案

解决方案分为四个步骤(以下我将其中一个页面名字叫做:DemoPage):

  1. 第一步将tab里面的页面DemoPage需要实现 AutomaticKeepAliveClientMixin
  2. 第二步设置DemoPage 实现代码 bool get wantKeepAlive => true;
  3. 第三步DemoPage 里面实现 Widget build(BuildContext context) 调用 super.build(context);
  4. 第四步 PageView.builder 设置NeverScrollableScrollPhysics(),//禁止页面左右滑动切换 (可选,好像这部不设置也没关系,具体看实际情况来)

具体 代码如下:

1、class _DemoPageState extends State<DemoPage> with AutomaticKeepAliveClientMixin{//要点1

2、 @override
bool get wantKeepAlive => true;//要点2 3、@override
Widget build(BuildContext context) {
super.build(context);//要点3 4、使用PageView构建
PageView.builder(//要点1
physics: NeverScrollableScrollPhysics(),//禁止页面左右滑动切换
controller: _pageController,
onPageChanged: _pageChanged,//回调函数
itemCount: _pages.length,
itemBuilder: (context,index)=>_pages[index]
)

参看文献:

https://blog.csdn.net/tgbus18990140382/article/details/81181879(这个好像没起什么作用啊)

https://stackoverflow.com/questions/52598900/flutter-bottomnavigationbar-rebuilds-page-on-change-of-tab

https://blog.csdn.net/weixin_34121282/article/details/88241346

最新文章

  1. 【记录】AutoMapper Project To not support ResolveUsing
  2. 调用DiscuzNT webApi 注册 登录 发帖
  3. Hibernate配置Log4J,很有参考价值的
  4. MySQL全文索引应用简明教程
  5. Map小记
  6. Java基础(62):Eclipse调试(Debug)的10条技巧(转)
  7. R(四): R开发实例-map分布图
  8. 如何修正导入模型的旋转? How do I fix the rotation of an imported model?
  9. Delphi ORD
  10. winform button设计(一)
  11. 创建用于编译和运行Java程序的批处理文件
  12. OCP-1Z0-051-题目解析-第11题
  13. windows做时间服务器,linux和windows时间同步
  14. Spark ML下实现的多分类adaboost+naivebayes算法在文本分类上的应用
  15. NoSQLBooster for MongoDB的基本使用
  16. react_app 项目开发 (6)_后台服务器端-node
  17. ModelAndView返回json对象的方法
  18. Centos-6.5搭建oracle11g RAC集群
  19. Shapley值的一个应用
  20. centos6.8下配置https服务器

热门文章

  1. linux下根据根据进程号查端口、根据端口号查进程号汇总,以及netstat的相关资料(工作中匮乏的知识)
  2. UVA - 143 Orchard Trees (点在三角形内)
  3. Gerrit(1): Manage Projects
  4. CDH5..4.7+phoenix实现查询HBase异常:java.sql.SQLException: ERROR 1102 (XCL02): Cannot get all table regions
  5. 最小生成树--Prim及Kruskal
  6. 关于Ms Sql server 表列等是否存在
  7. git提交遇到.suo文件无法提交的问题
  8. SQL Server2012 Offset Fetch子句 分页查询
  9. (数据科学学习手札57)用ggplotly()美化ggplot2图像
  10. 自学Oracle心得