Flutter-AutomaticKeepAliveClientMixin

Flutter切换tab后保留tab状态 概述 Flutter中为了节约内存不会保存widget的状态,widget都是临时变量。当我们使用TabBar,TabBarView是我们就会发现,切换tab,initState又会被调用一次。

怎么为了让tab一直保存在内存中,不被销毁?

添加AutomaticKeepAliveClientMixin,并设置为true,这样就能一直保持当前不被initState了。

class TicketListViewState extendsState with AutomaticKeepAliveClientMixin{ 

@override

void initState(){

super.initState();  

}

@override

Widget build(BuildContext context){

super.build(context);

return new SmartRefresher(        

enablePullDown:true,        

enablePullUp:true,       

 onRefresh: _onRefresh,        

controller: refreshController,        

child: ListView.builder(         

 itemCount: _result.length,         

 itemBuilder: (context, index) {returngetItem(_result[index]);          },        )); 

 }

//不会被销毁,占内存中@overridebool get wantKeepAlive =>true;

}

如果不起作用

@override Widget build(BuildContext context){

super.build(context);//必须添加.....        ));

官方解释

///A mixin with convenience methodsforclients of [AutomaticKeepAlive]. Used///with [State] subclasses.//////Subclasses must implement [wantKeepAlive],andtheir [build] methods must///call`super.build`(thereturnvalue will alwaysreturnnull,andshould be///ignored).

你可能感兴趣的:(Flutter-AutomaticKeepAliveClientMixin)