flutter tab切換頁面防止重置


問題描述:TabBar 配合TabBarView切換頁面,然后每個頁面是一個listview加載數據,但是切換頁面后listview的數據會被重置,

重新被加載了,解決辦法使用 with AutomaticKeepAliveClientMixin  實現  @protected bool get wantKeepAlive=>true; 即可,

可能會出現紅色報錯,直接忽視

需要添加下面這個,如果沒有analysis_options.yaml這個文件的話

https://github.com/flutter/flutter/blob/master/analysis_options.yaml#L24

class _OpenLogListComponentState extends State<OpenLogListComponent> with AutomaticKeepAliveClientMixin{
 
  @protected
  bool get wantKeepAlive=>true;
  //其他邏輯
}

方法二: 使用IndexedStack存儲頁面

_body = IndexedStack(
  children: <Widget>[
    BookHousePage(),
    FunctionPage(),
    ChatPage(),
    MinePage()
  ],
  index: _currentPageIndex,
);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM