保持块构建器内的有状态小部件的状态

时间:2021-07-22 17:00:25

标签: flutter bloc

我在 bloc builder 中有一个有状态的小部件,我的问题是我是否可以在重建之间保持小部件的状态?因为每次触发 bloc 构建器时,都会重置有状态的小部件状态。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用 AutomaticKeepAliveClientMixin

将它与您的州类一起使用。 您必须在状态类中覆盖 wantKeepAlive 方法。 在您的情况下返回 true

例如

class _FooWidgetState extends State<FooWidget> with AutomaticKeepAliveClientMixin {
  @override
  void initState() {
    super.initState();
  }
  
  @override
  Widget build(BuildContext context) {
    // your build method
  }
  
  @override
  bool wantKeepAlive => true;
}