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,
);

 

posted @ 2019-02-21 15:56  星辰之力  阅读(3817)  评论(0编辑  收藏  举报