flutter實(shí)現(xiàn)切換頁(yè)面緩存
本文實(shí)例為大家分享了flutter實(shí)現(xiàn)切換頁(yè)面緩存的具體代碼,供大家參考,具體內(nèi)容如下
一、實(shí)現(xiàn)底部導(dǎo)航欄切換頁(yè)面緩存
實(shí)現(xiàn)底部導(dǎo)航欄切換頁(yè)面緩存需要在pubspc.yamal中導(dǎo)入proste_indexed_stack
插件,這個(gè)插件可以實(shí)現(xiàn)懶加載,比起使用IndexedStack
包裹body
實(shí)現(xiàn),性能更好。
dependencies:
#懶加載的層疊組件 proste_indexed_stack: ?//不加版本號(hào)可獲取最新版本
實(shí)現(xiàn)底部導(dǎo)航切換頁(yè)面緩存只需將body
用ProsteIndexedStack
包裹一層既可以,注意ProsteIndexedStack
的children
是IndexedStackChild
類(lèi)型的,所以中的每一個(gè)children
的每一項(xiàng)都需要用IndexedStackChild
包裹
示例:
import 'package:flutter/material.dart'; ... //其他需要import的內(nèi)容省略 class RootPage extends StatefulWidget { ? @override ? _RootPageState createState() => _RootPageState(); } class _RootPageState extends State<RootPage> { ? //底部導(dǎo)航欄數(shù)組 ? final items = [ ? ? BottomNavigationBarItem( ? ? ? ? icon: Icon(Icons.home),label: '首頁(yè)',tooltip: '' ? ? ), ? ? BottomNavigationBarItem( ? ? ? ? icon: Icon(Icons.music_note),label: '音樂(lè)',tooltip: '' ? ? ), ? ? BottomNavigationBarItem( ? ? ? ? icon: Icon(Icons.slow_motion_video),label: '短視頻',tooltip: '' ? ? ), ? ? BottomNavigationBarItem( ? ? ? ? icon: Icon(Icons.account_circle_outlined),label: '我的',tooltip: '' ? ? ), ? ]; ? //底部導(dǎo)航欄頁(yè)面 ? final bodyList = [ ? ? IndexedStackChild(child: HomePage()), ? ? IndexedStackChild(child: MusicPage()), ? ? IndexedStackChild(child: TinyVideoPage()), ? ? IndexedStackChild(child: ProfilePage()), ? ]; ? //當(dāng)前選中頁(yè)面索引 ? int _currentIndex = 0; ? //底部導(dǎo)航欄切換 ? void _onTap(int index) { ? ? setState(() { ? ? ? _currentIndex = index; ? ? }); ? } ? @override ? Widget build(BuildContext context) { ? ? return Scaffold( ? ? ? bottomNavigationBar: BottomNavigationBar( ? ? ? ? items: items, ? ? ? ? currentIndex: _currentIndex, ?//當(dāng)前選中標(biāo)識(shí)符 ? ? ? ? onTap: _onTap, ? ? ? ? type: BottomNavigationBarType.fixed, ? ? ? ), ? ? ? //ProsteIndexedStack包裹,實(shí)現(xiàn)底部導(dǎo)航切換時(shí)保持原頁(yè)面狀態(tài) ? ? ? body: ProsteIndexedStack( ? ? ? ? index: _currentIndex, ? ? ? ? children: bodyList, ? ? ? ), ? ? ); ? } }
二、實(shí)現(xiàn)頂部tab切換頁(yè)面緩存
頂部tab切換頁(yè)面緩存可使用AutomaticKeepAliveClientMixin
實(shí)現(xiàn),只需在頁(yè)面的state
中混入AutomaticKeepAliveClientMixin
,然后重寫(xiě)wantKeepAlive
為true
即可。
做了以上配置,你如果在build
中 print
一下,當(dāng)你切換 tabbar 時(shí),print
就不會(huì)打印,也就實(shí)現(xiàn)了頁(yè)面保持狀態(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), ? ? ? ? )); ? } }
文章只介紹了如何實(shí)現(xiàn)切換頁(yè)面緩存,其他相關(guān)具體頁(yè)面實(shí)現(xiàn)在這里就不贅述了,有需要的可以自己實(shí)現(xiàn)一下試一試。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開(kāi)發(fā)使用Messenger及Handler進(jìn)行通信的方法示例
這篇文章主要介紹了Android開(kāi)發(fā)使用Messenger及Handler進(jìn)行通信的方法,結(jié)合實(shí)例形式分析了Android使用Messenger及Handler定義客戶端與服務(wù)器端實(shí)現(xiàn)通信的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12Android使用API實(shí)現(xiàn)圖像扭曲效果示例
這篇文章主要介紹了Android使用API實(shí)現(xiàn)圖像扭曲效果,涉及Android坐標(biāo)運(yùn)算與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android Handler 機(jī)制實(shí)現(xiàn)原理分析
本文主要介紹 Android Handle機(jī)制實(shí)現(xiàn)的原理,這里整理了詳細(xì)的關(guān)于Handler的資料以及工作流程和實(shí)際應(yīng)用,有興趣的小伙伴可以參考下2016-08-08Android中findViewById返回為空null的快速解決辦法
這篇文章主要介紹了Android中findViewById返回為空null的快速解決辦法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Android 多線程的實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 多線程的實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,這里提供三種方法,幫助大家掌握這部分內(nèi)容,需要的朋友可以參考下2017-08-08Android studio刪除Android項(xiàng)目方法
在本篇內(nèi)容里我們給大家介紹的是關(guān)于Android studio刪除Android項(xiàng)目方法和步驟,需要的可以學(xué)習(xí)下。2018-12-12解決android studio 打包發(fā)現(xiàn)generate signed apk 消失不見(jiàn)問(wèn)題
這篇文章主要介紹了解決android studio 打包發(fā)現(xiàn)generate signed apk 消失不見(jiàn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09詳解RecyclerView設(shè)置背景圖片長(zhǎng)寬一樣(以GridLayoutManager為例)
這篇文章主要介紹了詳解RecyclerView設(shè)置背景圖片長(zhǎng)寬一樣(以GridLayoutManager為例),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Android實(shí)現(xiàn)音樂(lè)播放器歌詞顯示效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)音樂(lè)播放器歌詞顯示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Android中使用LayoutInflater要注意的一些坑
LayoutInflater類(lèi)在我們?nèi)粘i_(kāi)發(fā)中經(jīng)常會(huì)用到,最近在使用中就遇到了一些問(wèn)題,所有下面這篇文章主要給大家總結(jié)了關(guān)于Android中使用LayoutInflater要注意的一些坑,希望通過(guò)這篇能讓大家避免走一些彎路,需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看吧。2017-04-04