【问题标题】:Keep state of statefull widget that is inside a bloc builder保持块生成器内的有状态小部件的状态
【发布时间】:2021-09-29 23:21:41
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: flutter bloc


    【解决方案1】:

    您可以使用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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2018-10-10
      • 2020-04-15
      • 2021-12-24
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      相关资源
      最近更新 更多