flutter ListView KeepAlive after some scroll

后端 未结 4 653
抹茶落季
抹茶落季 2020-12-11 18:37

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

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 19:07

    As stated by AutomaticKeepAliveClientMixin and Remi's answer,

    Subclasses must implement wantKeepAlive, and their build methods must call super.build (the return value will always return null, and should be ignored).

    Therefore, change your build method to:

    class Foo extends StatefulWidget {
      @override
      FooState createState() {
        return new FooState();
      }
    }
    
    class FooState extends State with AutomaticKeepAliveClientMixin {
      @override
      Widget build(BuildContext context) {
        super.build(context);
        return Container(
    
        );
      }
    
      @override
      bool get wantKeepAlive => true;
    }
    

提交回复
热议问题