首页 > 解决方案 > StatefulWidget项的Listview

问题描述

一旦 StatefulWidget 被释放,(item out of screen)如何获取 StatefulWidget 的状态?

我实际上设置了一个动画列表,但我认为这是同样的问题。可能更新列表可能会解决问题。但是怎么做?

我只想要相同的状态。

标签: flutter

解决方案


State需要保留需要使用AutomaticKeepAliveClientMixin的 .

这是一个例子:

class Foo extends StatefulWidget {
  @override
  FooState createState() {
    super.build(context);
    return new FooState();
  }
}

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

    );
  }

  @override
  bool get wantKeepAlive => true;
}

Foo即使这样的小部件将屏幕留在ListView


推荐阅读