flutter 页面缓存

flutter页面缓存方法:

混入AutomaticKeepAliveClientMixin部件设置页面缓存:

//在动态部件内
class Page1 extends StatefulWidget {
  @override
  _Page1State createState() => _Page1State();
}
//混入AutomaticKeepAliveClientMixin 设置缓存
class _Page1State extends State<MyHomePage> with AutomaticKeepAliveClientMixin {
  //重写keepAlive为ture  开启缓存功能。
  @override
  bool get wantKeepAlive => true;
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('缓存页面')),
      boby: IndexedStack(
		 //用来显示第index个child,
		 index: childIndex,
		 children: <Widget>['这里面存放你需要缓存的页面内容'],
	  )
    )
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章