当前位置: 首页 > news >正文

flutter中使用缓存

前言

在flutter项目中使用ListView或者PageView等有滚动条组件的时候,切换页面的时候,再切换回来会丢失之前的滑动状态,这个时候就需要需要使用缓存功能

缓存类

import 'package:flutter/material.dart';class KeepAliveWrapper extends StatefulWidget {const KeepAliveWrapper({Key? key,  this.child, this.keepAlive = true}): super(key: key);final Widget? child;final bool keepAlive;State<KeepAliveWrapper> createState() => _KeepAliveWrapperState();
}class _KeepAliveWrapperState extends State<KeepAliveWrapper>with AutomaticKeepAliveClientMixin {Widget build(BuildContext context) {return widget.child!;}bool get wantKeepAlive => widget.keepAlive;  
}

使用

在需要缓存的组件使用KeepAliveWrapper包裹一下即可

@overrideWidget build(BuildContext context) {return KeepAliveWrapper(child: Scaffold(body: Stack(children: [_homePage(), _appBar()],),));}
http://www.lryc.cn/news/185742.html

相关文章:

  • 京东数据分析平台:9月中上旬白酒消费市场数据分析
  • Linux安装 spark 教程详解
  • 动态内存管理函数(malloc,calloc,realloc,free)
  • 云表|都有生产管理模块,MES和ERP有什么不同,该如何选择
  • C语言 - 数组
  • Vue 中的插槽(Slot),有什么用,不同插槽的区别?
  • Linux登录自动执行脚本
  • 架构方法、模型、范式、治理
  • Linux 安全 - 内核提权
  • 数字三角形加强版题解(组合计数+快速幂+逆元)
  • MySQL:主从复制-基础复制(6)
  • 盒子模型的基础
  • Go复合类型之数组类型
  • rust闭包
  • 通过位运算,实现单字段标识多个状态位
  • ALSA pcm接口的概念解释
  • logging的基本使用教程
  • ds套dp——考虑位置转移or值域转移:CF1762F
  • stm32的GPIO寄存器操作以及GPIO外部中断,串口中断
  • 生成对抗网络入门案例
  • 多头注意力机制
  • Qt + FFmpeg 搭建 Windows 开发环境
  • [网鼎杯 2020 白虎组]PicDown python反弹shell proc/self目录的信息
  • SDL2绘制ffmpeg解析的mp4文件
  • 决策树C4.5算法的技术深度剖析、实战解读
  • LLMs Python解释器程序辅助语言模型(PAL)Program-aided language models (PAL)
  • 【12】c++设计模式——>单例模式练习(任务队列)
  • Python之函数、模块、包库
  • SQL创建与删除索引
  • 网络协议--链路层