【问题标题】:Remove item from ListView builder from a child widget从子小部件的 ListView 构建器中删除项目
【发布时间】:2021-08-22 18:40:22
【问题描述】:

我需要能够从子小部件中删除列表视图中的特定项目。然而,让这件事变得困难的是 ontap() 删除功能是在子小部件中定义的。

如何从子窗口更改父窗口小部件的状态?

父 ListView 构建器

ListView.builder(
  shrinkWrap: true,
  physics:
      BouncingScrollPhysics(),
  itemCount: snapshot
      .data.datas.length,
  itemBuilder:
      (context, index) {
    if (snapshot
            .data
            .datas[index]
            .daysToOccurrence >=
        0) {
      if (_isSearching ==
          false) {
        return OrganizationList(
           
            id: snapshot
                .data
                .datas[index]
                .id);
      } else {
        return snapshot
                .data
                .datas[index]
                .id
                .toLowerCase()
                .contains(
                    _searchText
                        .toLowerCase())
            ? OrganizationList(
                
                id: snapshot
                    .data
                    .datas[
                        index]
                    .id,
              )
            : Container();
      }
    } else {
      return Container();
    }
  }),

OrganizationList 小部件(在子小部件中定义)

Align(
    alignment: Alignment.bottomLeft,
    child: GestureDetector(
      onTap: () async {
        setState(() {
          _isLoading = true;
        });
        await _organizationBloc
            .delete(widget.id);

        setState(() {
          _isLoading = false;
        });
      },
      child: Text(
        "Delete",
        textAlign: TextAlign.center,
        style: TextStyle(
          color: Colors.black54,
        ),
      ),
    ))

【问题讨论】:

  • 尝试 voidCallback 作为子小部件中的参数
  • 好的,试一试。但这如何与流一起使用?具体来说,快照不是全局列表
  • 我应该澄清一下这个 ListView.Builder 从 StreamBuilder 获取数据

标签: flutter


【解决方案1】:

找到了解决办法。诀窍是在父窗口小部件中定义一个删除方法,将 ObjectKey() 传递给子窗口小部件,并将 voidCallback 参数传递给子窗口小部件。

voidCallback 将映射到父窗口小部件中的删除方法,您可以从子窗口小部件中调用该函数。

父小部件

  void _deleteItem(Organization item, List<Organization> organizationList) {
    organizationList.remove(item);
    setState(() {});
  }

更新了 ListViewBuilder

ListView.builder(
  key: UniqueKey(),
  shrinkWrap: true,
  physics:
      BouncingScrollPhysics(),
  itemCount: snapshot
      .data.datas.length,
  itemBuilder:
      (context, index) {
    if (snapshot
            .data
            .datas[index]
            .daysToOccurrence >=
        0) {
      if (_isSearching ==
          false) {
        return OrganizationList(
           key: ObjectKey(
            snapshot.data
                    .datas[
                index]),
                onDeleteClicked: () =>
              _deleteItem(
                  snapshot.data.datas[
              index], snapshot.data.datas),
            id: snapshot
                .data
                .datas[index]
                .id);
      } else {
        return snapshot
                .data
                .datas[index]
                .id
                .toLowerCase()
                .contains(
                    _searchText
                        .toLowerCase())
            ? OrganizationList(
                
                id: snapshot
                    .data
                    .datas[
                        index]
                    .id,
              )
            : Container();
      }
    } else {
      return Container();
    }
  }),

然后在具有删除功能的子小部件中,我可以调用它:

widget.onDeleteClicked();

【讨论】:

    猜你喜欢
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多