【问题标题】:Correct way to manage state on dynamic widgets in Flutter在 Flutter 中管理动态小部件状态的正确方法
【发布时间】:2021-10-08 06:25:43
【问题描述】:

我在 sqlite 中有一个类别列表,我在 streambuilder 中获得了这些行,然后,我创建了一个开关小部件列表。在每个切换小部件的切换事件中,我更改源值并调用 setState 方法,但这会导致构建事件的执行并重置所有切换小部件的值。之后,我更改了代码以将所有小部件存储在一个变量中,如果小部件存在,则在 streambuild 中存储,返回小部件列表,这几乎可以正常工作,这会更新源中的小部件值,但是,如果它们是假的,小部件看起来像.

有人知道吗?

问候

           StreamBuilder<List<Category>>(   <--- this code is in build event
              stream: _catBloc.categoriesStream,
              builder: (BuildContext context, AsyncSnapshot<List<Category>> snapshot) {
                if(!snapshot.hasData) {
                  return Container(
                    height: _size.height,
                    width: _size.width,
                    child: Center(
                      child: CircularProgressIndicator(),
                    ),
                  );
                } else if(_categoriesLinked.isEmpty){ <-- this is a map with the list of id and name
                  column = _createCategories(snapshot.data, widthSwitch);
                  return column;
                 } else {
                   return column;
                 }
              },
            ),


Container(  <-- this is inside _createCategories method
      width: width,
      child: Row(
        children: [
          FlutterSwitch(
            width: _size.width * 0.06,
            height: _size.height * 0.02,
            toggleSize: 20.0,
            value: _categoriesLinked[e.id],
            showOnOff: false,
            padding: 2.0,
            activeColor: Color.fromRGBO(88, 203, 143, 0.25),
            inactiveColor: Color.fromRGBO(224, 233, 240, 0.50),
            activeToggleColor: Color.fromRGBO(88, 203, 143, 1.0),
            inactiveToggleColor: Color.fromRGBO(78, 88, 96, 0.50),
            onToggle: (val) => setState(() {
              _categoriesLinked[e.id] = val;
            }),
          ),
          SizedBox(width: _size.width * 0.01,),
          Flexible(child: Text("${ e.id}. ${ e.name}"))
        ]
      ),
    )

【问题讨论】:

    标签: android flutter dart setstate stream-builder


    【解决方案1】:

    在这种情况下,您应该重构您的代码,以便将开关创建为另一个小部件,使它们只能自己重建。

    对于开关的状态,您可以简单地使用GlobalKey&lt;MySwitchState&gt; 从父级获取它,以便在onPressed 方法期间访问子级状态,例如:mySwitchKey.currentState

    您绝对应该避免在方法中构建有状态的小部件。如果您觉得需要一种方法,请创建一个新的小部件。

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 2020-06-08
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 2019-09-23
      • 2019-06-10
      • 2019-04-26
      相关资源
      最近更新 更多