如果你用过BottomNavigationBar、TabBar、还有Drawer,你就会发现,在切换页面之后,原来的页面状态就会丢失。

要是上一页有一个数据列表,很多数据,你滚动到了下头,切换页面后,想再看一下下头的数据,但是Flutter给你重回页面了。。。

这谁能顶得住啊。

看了一下解释,原来Flutter中为了节约内存不会保存widget的状态,widget都是临时变量。

不过还是有很多办法解决的,网上传言用 AutomaticKeepAliveClientMixin 配合

@override
bool get wantKeepAlive => true;

服用,效果会达到保持状态。但是这个方法貌似只对TabBar起作用,BottomNavigationBar 对 AutomaticKeepAliveClientMixin 有免疫,不起作用。

后来一顿猛于虎的番羽土啬操作后,还是找到了解决方案,完美实现BottomNavigationBar底部导航栏切换,状态保持。

答案就是body使用IndexedStack即可。

参考我的代码:

import 'package:flutter/material.dart';

import './pages/home_page.dart';
import './pages/book_page.dart';
import 'package:bottom_nav_bar_test/pages/movie_page.dart';
import 'package:bottom_nav_bar_test/pages/music_page.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bottom Navigation',
home: Main(),
theme: ThemeData(primaryColor: Colors.orange),
);
}
} class Main extends StatefulWidget {
@override
_MainState createState() => _MainState();
} class _MainState extends State<Main> {
int _currentIndex = 0;
final List<Widget> _children = [Home(), Book(), Music(), Movie()]; final List<BottomNavigationBarItem> _list = <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
//backgroundColor: Colors.orange
),
BottomNavigationBarItem(
icon: Icon(Icons.book),
title: Text('Book'),
//backgroundColor: Colors.orange
),
BottomNavigationBarItem(
icon: Icon(Icons.music_video),
title: Text('Music'),
//backgroundColor: Colors.orange
),
BottomNavigationBarItem(
icon: Icon(Icons.movie),
title: Text('Movie'),
//backgroundColor: Colors.orange
),
]; @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Bottom Navigation'),
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
onTap: onTabTapped,
currentIndex: _currentIndex,
items: _list,
),
//body: _children[_currentIndex],
body: IndexedStack(
index: _currentIndex,
children: _children,
),
);
} void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
}

参考 https://stackoverflow.com/questions/53011686/flutter-automatickeepaliveclientmixin-is-not-working-with-bottomnavigationbar 中 hfimy 的回答。

Flutter - BottomNavigationBar底部导航栏切换后,状态丢失的更多相关文章

  1. Flutter - BottomNavigationBar底部导航栏切换后,状态丢失。底部

    import 'package:flutter/material.dart'; import './pages/home_page.dart'; import './pages/book_page.d ...

  2. Flutter - TabBar导航栏切换后,状态丢失

    上一篇讲到了 Flutter - BottomNavigationBar底部导航栏切换后,状态丢失 里面提到了TabBar,这儿专门再写一下吧,具体怎么操作,来不让TabBar的状态丢失.毕竟大家99 ...

  3. Flutter实战视频-移动电商-04.底部导航栏切换效果

    04.底部导航栏切换效果 博客地址: https://jspang.com/post/FlutterShop.html#toc-291 我们要做的效果图: 新建四个页面 home_page.dart ...

  4. mui底部导航栏切换分页

    使用Hbuilder的mui框架开发移动端非常便利.高效: 底部导航栏切换功能也是移动APP开发中必须实现的: 引入mui文件.下面会用到jquery,同时引进 <link href=" ...

  5. 【Flutter学习】基本组件之BottomNavigationBar底部导航栏

    一,概述 BottomNavigationBar即是底部导航栏控件,显示在页面底部的设计控件,用于在试图切换,底部导航栏包含多个标签.图标或者两者搭配的形式,简而言之提供了顶级视图之间的快速导航. 二 ...

  6. Flutter - 创建底部导航栏

    之前写过的一篇文章介绍了 Flutter - 创建横跨所有页面的侧滑菜单, 这次就一起来学习一下底部导航栏. 底部导航栏在ios平台上非常常见,app store就是这样的风格.还有就是大家最常用的微 ...

  7. MUI底部导航栏切换效果

    首先是html代码: <nav class="mui-bar mui-bar-tab"> <a href="view/templates/home/ho ...

  8. Android学习总结——输入法将BottomNavigationBar(底部导航栏)顶上去的问题

    在应用清单中给当前<Activity>设置: android:windowSoftInputMode="adjustPan" 关于android:windowSoftI ...

  9. 底部导航栏使用BottomNavigationBar

    1.github地址 https://github.com/zhouxu88/BottomNavigationBar 2.基本使用 2,1添加依赖 implementation 'com.ashokv ...

随机推荐

  1. (后台)org.apache.catalina.connector.ClientAbortException: null

    比如错误日志是这样的: org.apache.catalina.connector.ClientAbortException: null 那么问题基本上就是服务器准备进行response的时候,发现连 ...

  2. The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured

    springboot 8080端口被占用报错:The Tomcat connector configured to listen on port 8080 failed to start. The p ...

  3. element-ui的回调函数Events的用法

    做轮播的时候想用这个change回调函数,但是官方文档上竟然就只列了这么一行东西,完全没有示例代码(也可能我没找到哈) 鼓捣了半天,东拼西凑终于找到了靠谱的使用方法,其实很简单 在轮播组件上加上@ch ...

  4. php解决前后端验证字符串长度不一致

    前端代码 function getStrleng(str){ var myLen =0; for(var i=0;i<str.length;i++){ if(str.charCodeAt(i)& ...

  5. 安全之路 —— C++实现进程守护

    简介 所谓进程守护,就是A进程为了保护自己不被结束,创建了一个守护线程来保护自己,一旦被结束进程,便重新启动.进程守护的方法多被应用于恶意软件,是一个保护自己进程的一个简单方式,在ring3下即可轻松 ...

  6. SQL Server 链接服务器连接 SQLite数据库文件

    SQL Server数据库允许通过数据库驱动程序连接各类数据库并进行操作.以下是在SQL Server 2012 R2中建立SQLite的链接服务器. 一.下载SQLite数据库的ODBC驱动程序: ...

  7. asp.net core中使用HttpClient实现Post和Get的同步异步方法

     准备工作 1.visual studio 2015 update3开发环境 2.net core 1.0.1 及以上版本  目录 1.HttpGet方法 2.HttpPost方法 3.使用示例 4. ...

  8. 设计 MySQL 数据表的时候一般都有一列为自增 ID,这样设计原因是什么,有什么好处?

    知乎采集: MyISAM/InnoDB默认用B-Tree索引(可理解为"排好序的快速查找结构"). InnoDB中,主索引文件上直接存放该行数据,称为聚簇索引.次索引指向对主键的引 ...

  9. IO流_PrintWriter(字符打印流)与PrintStream(字节打印流)

    PrintStream:  1.提供了打印方法可以对多种数据类型值进行打印,并保持数据的表示形式  2.它不抛IOException  3.构造函数接受三种类型的值:  字符串路径  File对象   ...

  10. BZOJ1023:[SHOI2008]cactus仙人掌图(圆方树,DP,单调队列)

    Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus). 所谓简单回路就是指在图上不重复经过任何一个顶点 ...