flutter ListView KeepAlive after some scroll

☆樱花仙子☆ 提交于 2019-12-13 16:08:20

问题


I want to keepAlive my widgets which are already rendered in ListView. I was tried with addAutomaticKeepAlives:true properties which provide by ListView class.

Here is my sample code which I was used. Same issue in SliverChildBuilderDelegate delegate which provide by SliverList.

ListView.builder(
    itemBuilder: (context,index){
      return Card(
        child: Container(
          child: Image.asset("images/${index+1}.jpg",fit: BoxFit.cover,),
          height: 250.0,
        ),
      );
    },
    addAutomaticKeepAlives: true,
    itemCount:40 ,
);

回答1:


For automaticKeepAlive to work, each item that needs to be kept alive must send a specific notification.

A typical way to fire such notification is using AutomaticKeepAliveClientMixin

class Foo extends StatefulWidget {
  @override
  FooState createState() {
    return new FooState();
  }
}

class FooState extends State<Foo> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    return Container(

    );
  }

  @override
  bool get wantKeepAlive => true;
}



回答2:


You can try looking at the cacheExtent property on listview builder as well. Setting that to a value to cover your items will keep them all alive. Thanks to Remi above. I had no idea the items needed keepAlive when using it on the list - that was not in the flutter doco previously...



来源:https://stackoverflow.com/questions/52541172/flutter-listview-keepalive-after-some-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!