flutter實現(xiàn)切換頁面緩存
本文實例為大家分享了flutter實現(xiàn)切換頁面緩存的具體代碼,供大家參考,具體內(nèi)容如下
一、實現(xiàn)底部導航欄切換頁面緩存
實現(xiàn)底部導航欄切換頁面緩存需要在pubspc.yamal中導入proste_indexed_stack
插件,這個插件可以實現(xiàn)懶加載,比起使用IndexedStack
包裹body
實現(xiàn),性能更好。
dependencies:
#懶加載的層疊組件 proste_indexed_stack: ?//不加版本號可獲取最新版本
實現(xiàn)底部導航切換頁面緩存只需將body
用ProsteIndexedStack
包裹一層既可以,注意ProsteIndexedStack
的children
是IndexedStackChild
類型的,所以中的每一個children
的每一項都需要用IndexedStackChild
包裹
示例:
import 'package:flutter/material.dart'; ... //其他需要import的內(nèi)容省略 class RootPage extends StatefulWidget { ? @override ? _RootPageState createState() => _RootPageState(); } class _RootPageState extends State<RootPage> { ? //底部導航欄數(shù)組 ? 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包裹,實現(xiàn)底部導航切換時保持原頁面狀態(tài) ? ? ? body: ProsteIndexedStack( ? ? ? ? index: _currentIndex, ? ? ? ? children: bodyList, ? ? ? ), ? ? ); ? } }
二、實現(xiàn)頂部tab切換頁面緩存
頂部tab切換頁面緩存可使用AutomaticKeepAliveClientMixin
實現(xiàn),只需在頁面的state
中混入AutomaticKeepAliveClientMixin
,然后重寫wantKeepAlive
為true
即可。
做了以上配置,你如果在build
中 print
一下,當你切換 tabbar 時,print
就不會打印,也就實現(xiàn)了頁面保持狀態(tài)。
示例:
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), ? ? ? ? )); ? } }
文章只介紹了如何實現(xiàn)切換頁面緩存,其他相關具體頁面實現(xiàn)在這里就不贅述了,有需要的可以自己實現(xiàn)一下試一試。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android開發(fā)使用Messenger及Handler進行通信的方法示例
這篇文章主要介紹了Android開發(fā)使用Messenger及Handler進行通信的方法,結(jié)合實例形式分析了Android使用Messenger及Handler定義客戶端與服務器端實現(xiàn)通信的相關操作技巧,需要的朋友可以參考下2017-12-12Android Handler 機制實現(xiàn)原理分析
本文主要介紹 Android Handle機制實現(xiàn)的原理,這里整理了詳細的關于Handler的資料以及工作流程和實際應用,有興趣的小伙伴可以參考下2016-08-08Android中findViewById返回為空null的快速解決辦法
這篇文章主要介紹了Android中findViewById返回為空null的快速解決辦法的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06解決android studio 打包發(fā)現(xiàn)generate signed apk 消失不見問題
這篇文章主要介紹了解決android studio 打包發(fā)現(xiàn)generate signed apk 消失不見問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09詳解RecyclerView設置背景圖片長寬一樣(以GridLayoutManager為例)
這篇文章主要介紹了詳解RecyclerView設置背景圖片長寬一樣(以GridLayoutManager為例),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12Android中使用LayoutInflater要注意的一些坑
LayoutInflater類在我們?nèi)粘i_發(fā)中經(jīng)常會用到,最近在使用中就遇到了一些問題,所有下面這篇文章主要給大家總結(jié)了關于Android中使用LayoutInflater要注意的一些坑,希望通過這篇能讓大家避免走一些彎路,需要的朋友可以參考學習,下面來一起看吧。2017-04-04