当前位置:网站首页>flutter如何实现切换页面缓存
flutter如何实现切换页面缓存
2022-07-29 15:46:00 【亿速云】
flutter如何实现切换页面缓存
本文小编为大家详细介绍“flutter如何实现切换页面缓存”,内容详细,步骤清晰,细节处理妥当,希望这篇“flutter如何实现切换页面缓存”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
一、实现底部导航栏切换页面缓存
实现底部导航栏切换页面缓存需要在pubspc.yamal中导入proste_indexed_stack插件,这个插件可以实现懒加载,比起使用IndexedStack包裹body实现,性能更好。
dependencies:
#懒加载的层叠组件proste_indexed_stack: //不加版本号可获取最新版本
实现底部导航切换页面缓存只需将body用ProsteIndexedStack包裹一层既可以,注意ProsteIndexedStack的children 是IndexedStackChild类型的,所以中的每一个children 的每一项都需要用IndexedStackChild包裹
示例:
import 'package:flutter/material.dart';... //其他需要import的内容省略class RootPage extends StatefulWidget { @override _RootPageState createState() => _RootPageState();}class _RootPageState extends State<RootPage> { //底部导航栏数组 final items = [ BottomNavigationBarItem( icon: Icon(Icons.home),label: '首页',tooltip: '' ), BottomNavigationBarItem( icon: Icon(Icons.music_note),label: '音乐',tooltip: '' ), BottomNavigationBarItem( icon: Icon(Icons.slow_motion_video),label: '短视频',tooltip: '' ), BottomNavigationBarItem( icon: Icon(Icons.account_circle_outlined),label: '我的',tooltip: '' ), ]; //底部导航栏页面 final bodyList = [ IndexedStackChild(child: HomePage()), IndexedStackChild(child: MusicPage()), IndexedStackChild(child: TinyVideoPage()), IndexedStackChild(child: ProfilePage()), ]; //当前选中页面索引 int _currentIndex = 0; //底部导航栏切换 void _onTap(int index) { setState(() { _currentIndex = index; }); } @override Widget build(BuildContext context) { return Scaffold( bottomNavigationBar: BottomNavigationBar( items: items, currentIndex: _currentIndex, //当前选中标识符 onTap: _onTap, type: BottomNavigationBarType.fixed, ), //ProsteIndexedStack包裹,实现底部导航切换时保持原页面状态 body: ProsteIndexedStack( index: _currentIndex, children: bodyList, ), ); }}二、实现顶部tab切换页面缓存
顶部tab切换页面缓存可使用AutomaticKeepAliveClientMixin实现,只需在页面的state中混入AutomaticKeepAliveClientMixin,然后重写wantKeepAlive为true即可。
做了以上配置,你如果在build 中 print 一下,当你切换 tabbar 时,print 就不会打印,也就实现了页面保持状态。
示例:
import 'package:flutter/material.dart';class ExamplePage extends StatefulWidget { @override _ExamplePageState createState() => _RecommendPageState();}class _ExmaplePageState extends State<ExamplePage> with AutomaticKeepAliveClientMixin { int count = 0; void add() { setState(() { count++; }); } @override bool get wantKeepAlive => true; @override void initState() { super.initState(); print('recommend initState'); } @override Widget build(BuildContext context) { super.build(context); return Scaffold( body:Center( child: Text('Example: $count', style: TextStyle(fontSize: 30)) ), floatingActionButton: FloatingActionButton( onPressed: add, child: Icon(Icons.add), )); }}读到这里,这篇“flutter如何实现切换页面缓存”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
边栏推荐
- 我把小程序搬到了自有App中,开发效率提升了5倍
- @RequestMapping注解最详细解析
- Property (Property Animation Animation), the basic use of Butterknife butter knife
- 【PCL学习笔记】点云处理常用的库和API(PCL库+Eigen)
- 【软考软件评测师】自动化测试章节上篇
- 面试官:设计原则有哪些?什么是里式替换原则?
- 一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
- 【微信小程序】组件使用及属性参考
- 【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)-- 组件UI
- 风格迁移篇----艺术风格转换的内容与风格解构
猜你喜欢

ES6 from entry to master # 11: the Map data type

教程篇(7.0) 07. 诊断和故障排除 * FortiClient EMS * Fortinet 网络安全专家 NSE 5
如何破坏单例?我说了好几种方式,面试官:没想到你真会

Compose要是不 `remember`,相关功能就实现不了了吗?

【微信小程序】组件使用及属性参考

支持百亿请求的微博广告运维技术实践

数字孪生万物可视 | 联接现实世界与数字空间

Google Play 政策更新 | 2022 年 7 月

This article penetrates the architecture design and cluster construction of the distributed storage system Ceph (hands-on)

网络知识大集合(最详细)与网络通信过程
随机推荐
See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices
Win11 最新补丁更新破坏了任务栏中的“开始”菜单
稳步向前不忘初心,沃尔沃的安全感来自公众的认可
【设计师必学】在SketchUp中Enscape的灯光照明技巧
zabbix邮件发送告警信息
ByteArrayOutputStream 类源码分析
【C语言刷题】Leetcode268丢失的数字
File management: the physical structure of files
Comparator和Comparable
R语言中给数据框增加分组/根据样本名称添加分组的代码
Qt learning on the first day
Qt combat | how to access the USB device information?
tcp的四次挥手(为什么三次握手和四次挥手)
MQTT over QUIC:下一代物联网标准协议为消息传输场景注入新动力
生产者消费代码
任正非:华为绝不会出售终端手机业务!
【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
显微镜LED补光可调光源
一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
兆易创新2021年将从长鑫存储采购3亿美元DRAM产品