使用bottomNavigationBar切换底部tab,再切换回来就会丢失之前的状态(重新渲染列表,丢失滚动条位置)。

解决方法

使用 AutomaticKeepAliveClientMixin

重写 bool get wantKeepAlive => true;

build方法中调用super.build(context);

class _MovieListState extends State<MovieList> with AutomaticKeepAliveClientMixin {
List movieList = new List(); @override
bool get wantKeepAlive => true; @override
Widget build(BuildContext context) {
super.build(context);
return ListView.builder(
itemCount: movieList.length,
itemBuilder: (context, index) {
return MovieItem(
item: movieList[index],
);
},
);
}
}

达到保存列表状态的效果

引用自官网的说明:

Subclasses must implement wantKeepAlive, and their build methods must call super.build (the return value will always return null, and should be ignored).

Then, whenever wantKeepAlive's value changes (or might change), the subclass should call updateKeepAlive.

The type argument T is the type of the StatefulWidget subclass of the State into which this class is being mixed.

参考链接:

AutomaticKeepAliveClientMixin mixin

Flutter保持页面状态AutomaticKeepAliveClientMixin的更多相关文章

  1. flutter 保持页面状态

    import 'package:flutter/material.dart'; import 'KeepAliveDemo.dart'; void main() => runApp(MyApp( ...

  2. Flutter 之页面状态保持

    一般情况下,我们使用tab切换的时候希望操作完毕之后,能够记住上个页面的状态, 但是使用Flutter的BottomNavigationBar的 时候默认是不记录页面状态的,即切换页面会导致重新加载. ...

  3. Flutter移动电商实战 --(16)切换后页面状态的保持AutomaticKeepAliveClientMixin

    底栏切换每次都重新请求是一件非常恶心的事,flutter 中提供了AutomaticKeepAliveClientMixin 帮我们完成页面状态保存效果. 1.AutomaticKeepAliveCl ...

  4. Flutter实战视频-移动电商-16.补充_保持页面状态

    16.补充_保持页面状态 修正一个地方: 设置了item的高度为380 横向列表为380.最终build的高度也增加了50为430. 增加了上面的高度以后,下面那个横线划掉的价格可以显示出来了. 但是 ...

  5. 16-Flutter移动电商实战-切换后页面状态的保持AutomaticKeepAliveClientMixin

    底栏切换每次都重新请求是一件非常恶心的事,flutter 中提供了AutomaticKeepAliveClientMixin 帮我们完成页面状态保存效果. 1.AutomaticKeepAliveCl ...

  6. 09 Flutter底部Tab切换保持页面状态的几种方法

    IndexedStack:保此所有页面的状态: AutomaticKeepAliveClientMixin:保此部分页面的状态: 修改的页面代码: 页面效果: Tabs.dart import 'pa ...

  7. 巧用location.hash保存页面状态

    在我们的项目中,有大量ajax查询表单+结果列表的页面,由于查询结果是ajax返回的,当用户点击列表的某一项进入详情页之后,再点击浏览器回退按钮返回ajax查询页面,这时大家都知道查询页面的表单和结果 ...

  8. Windows Phone开发(9):关于页面状态

    原文:Windows Phone开发(9):关于页面状态 按照一般做法,刚学会如何导航,还是不够的,因为要知道,手机里面的每个页面,就如同Web页面一样,是无状态的. 啥是无状态?如果我们玩过Web开 ...

  9. 四:理解Page类的运行机制(例:基于PageStatePersister的页面状态存取)

    有人说类似gridview datalist这样的控件最好不要用在高并发,IO大的网站中企业应用中为了快速开发到可以用一用因为这是一类"沉重"的组件我们姑且不谈这种看法的正确性(我 ...

随机推荐

  1. Python实现求1-1000以内的素数

    def func(): for i in range(2,1000): # count表示被整除的次数 count = 0 for j in range(1,i+1): if i%j==0: coun ...

  2. [LC] 46. Permutations

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  3. python库之-------Pandas

    包括两个数据结构:DataFrame和Series 官方文档地址: pandas https://pandas.pydata.org/pandas-docs/stable/index.html ser ...

  4. Logarithmic transformation|Data transfer|MASS|Box-Cox

    数据转换(Data transfer) 方差分析的前提是方差齐性,可以使用transfer改变方差使得方差变齐.不正态和outlier. Logarithmic transformation使方差聚合 ...

  5. particlesJS is not defined

    插件或者js文件在引入时需要注意引入顺序,每次都找很久的错误 一般引入min.js就可以,min.js意思就是压缩的js文件 引入时应该先加入min类的文件在引入配置文件 比如particles插件, ...

  6. 三、RabbitMQ安装

    安装前准备 Linux版本信息: 发行版本:CentOS Linux release 7.5.1804 (Core) 内核版本:Linux version 3.10.0-862.el7.x86_64 ...

  7. SpringBoot连接Oracle报错,找不到驱动类,application.properties文件中驱动类路径为红色

    pom.xml文件: <!-- oracle odbc --> <dependency> <groupId>com.oracle</groupId> & ...

  8. shell制作bin文件

    #!/bin/bash curdir=`pwd` tardir=tardir if [ -e $tardir ];then echo $tardir is exist.... false! exit ...

  9. MAVEN实现多环境搭建

    在实际的开发中,会遇到开发环境的不同(开发环境,测试环境,线上环境),会来回根据环境的不同修改配置文件,一不小心修改错误导致无法正常运行,故障排除导致开发效率低.使用maven可以根据环境的不同,自动 ...

  10. npm镜像源

    1.国内用户,建议将npm的注册表源设置为国内的镜像,可以大幅提升安装速度,先查看本机地址 npm config get registry 2.国内优秀npm镜像推荐及使用 淘宝npm镜像 ·搜索地址 ...