![](/img/trans.png)
[英]Flutter : Showing long widget list using ListView.builder() and ListTile
[英]How to Loop through a ListTile from ListView.builder in flutter
StreamBuilder(
stream: Firestore.instance
.collection('Recharge_Card')
.snapshots(),
//print an integer every 2secs, 10 times
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("Loading..");
}
return SizedBox(
height: _height / 1.9,
child: ListView.builder(
// itemExtent: 80.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot myCards =
snapshot.data.documents[index];
return Card(
elevation: 20.0,
child: ListTile(
onTap: () {
setState(() {
x = Text('Tapped');
});
},
leading: x,
title: Text(myCards['CardPin']),
trailing: Text(myCards['Value']),
),
);
},
),
);`
},
),
發生這種情況是因為所有卡片的x
都是相同的,您需要將這個人分開,我的建議是將x
放在DocumentSnapshot
中,所以您只能更改已點擊的卡片,如下所示:
StreamBuilder(
stream: Firestore.instance
.collection('Recharge_Card')
.snapshots(),
//print an integer every 2secs, 10 times
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("Loading..");
}
return SizedBox(
height: _height / 1.9,
child: ListView.builder(
// itemExtent: 80.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot myCards =
snapshot.data.documents[index];
return Card(
elevation: 20.0,
child: ListTile(
onTap: () {
setState(() {
myCards['x'] = Text('Tapped');
});
},
leading: myCards['x'],
title: Text(myCards['CardPin']),
trailing: Text(myCards['Value']),
),
);
},
),
);`
},
),
為您的卡添加密鑰並提供唯一值,在您的情況下,它是您的索引
Card(
key: ValueKey(index),
//....
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.