網站首頁 編程語言 正文
本文實例為大家分享了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), ? ? ? ? )); ? } }
文章只介紹了如何實現切換頁面緩存,其他相關具體頁面實現在這里就不贅述了,有需要的可以自己實現一下試一試。
原文鏈接:https://blog.csdn.net/weixin_47894857/article/details/119715051
相關推薦
- 2022-04-10 Pytest單元測試框架生成HTML測試報告及優化的步驟_python
- 2022-06-28 python神經網絡學習利用PyTorch進行回歸運算_python
- 2022-08-06 GIN的路由以及傳參問題_Golang
- 2023-11-23 nginx偽靜態try_files命令解讀
- 2023-12-25 Spring 之 @Cacheable 緩存使用教程
- 2021-12-06 centos7.6批量增加修改刪除虛擬網卡操作介紹_Linux
- 2022-03-03 text-overflow:ellipsis,當對象內文本溢出時顯示省略標記(...)
- 2022-09-07 Golang必知必會之Go?Mod命令詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支