首页 > 解决方案 > Flutter: Only the first item in the Stack is dismissible

问题描述

I am cracking my head over this one. I want all my items in my stack to be dismissible. I am able to show all my items but only the first item is dismissible. Why?

Here is the code snippet:

Widget build(BuildContext context) {
    return new StreamBuilder <QuerySnapshot>(
        stream: Firestore.instance.collection('records').document(uid).collection("pdrecords").snapshots(),
          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasData) {
              print("snapShot call");
              final int recordCount = snapshot.data.documents.length;
              print("got data. records: $recordCount");
              if (recordCount >0) {
                return new Stack (
                alignment: listSlideAnimation.value,
                children:
                    snapshot.data.documents.map((DocumentSnapshot document) {
                      count = count + multiplyFactor; print("count: $count");
                      index ++;
                      //print("data: ${document.data}");
                  return Dismissible(
                    resizeDuration: null,
                    dismissThresholds: _dismissThresholds(),
                    //background: new LeaveBehindView(),
                    key: ObjectKey(document.documentID) ,
                    onDismissed: (DismissDirection direction) {
                      direction == DismissDirection.endToStart
                        ? print("favourite")
                        : print("remove");

                          // Do stuff

                    },

                  child: 
                  new ListPDRecord(

                    id: document.documentID,
                    margin: listSlidePosition.value * count, //new EdgeInsets.only(bottom: 80.0) * count, //listSlidePosition.value * 5.5,
                    width: listTileWidth.value,
                    date: document["date"],

                  ),
                  );
                }).toList(),
              );
          } else {
             return NoDataListData();
          }
      } else {
            // No data
            return new NoDataListData();
      }
  });
 }
}

I suspect is the KEY and I have tried many variations of different keys such as manually increment of index, etc. but still, only the first item is dismissible.

Any pointers?

标签: firebasestackflutter

解决方案


经过多轮实验,我解决了以下几点:

  1. 您可以像上面一样在堆栈内生成小部件列表,但是
  2. 可解雇可能不会起作用,因为堆栈中的每个项目都有不同的边距,而顶部的边距最大。
  3. 最后,我必须将 Stack 更改为 ListView 并且可解雇现在适用于每个项目。

推荐阅读