我正在使用BlockBuilder,它包括一个ListTile。但是我单击一个项目,其他项目也单击。当我将其推送到新屏幕时,它是一项,但是我不知道为什么它不能彼此区分。
这是我在Android Studio中的代码:
Widget _buildListTile(ProductItemModel item) {
final bool alreadySaved = _saved.contains(item);
return ListTile(
title: Text(
item.name,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text("Price: " + item.price),
leading: Container(
width: 69,
height: 40,
decoration: BoxDecoration(border: Border.all(color: Colors.black54)),
child: Image.network(item.icon),
),
trailing: new Icon(
alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null,
),
// event ontap to catch like/ don't like
onTap: () {
setState(() {
if (alreadySaved) {
_saved.remove(item);
print("move"+item.name);
} else {
_saved.add(item);
print("add"+item.name);
}
});
},
);
}
该数据来自模型,但我无法理解。...
This is my api, i get data from this...
class ProductApiProvider {
Future<ProductModel> getListProduct() async {
//await Future.delayed(Duration(seconds: 2));
return ProductModel.fromJson([
{
"id": 1,
"name": "RTX 2070s",
"price" : "1.190 USD",
"icon" : "https://assets.pcmag.com/media/images/518866-nvidia-geforce-rtx-2080-founders-edition-7.jpg?width=810&height=456",
"isLove" : false
},
{
"id": 2,
"name": "RTX 2080",
"price" : "1.999 USD",
"icon" : "https://assets.pcmag.com/media/images/518013-yeah-it-s-a-beast.jpg?width=810&height=456",
"isLove" : false
},
{
"id": 3,
"name": "Mainboard Z390",
"price" : "499 USD",
"icon" : "https://assets.pcmag.com/media/images/610433-meet-the-asrock-z390-taichi.jpg?thumb=y&width=980&height=456",
},
{
"id": 4,
"name": "Main Z370",
"price" : "349 USD",
"icon" : "https://assets.pcmag.com/media/images/498346-image-aorus-jpg.jpg?width=810&height=456",
},
{
"id": 5,
"name": "I3 8100",
"price" : "199 USD",
"icon" : "https://assets.pcmag.com/media/images/465550-cpu-showdown-intel-core-i3-vs-i5.jpg?thumb=y&width=810&height=456"
},
]);
}
}
这是我的模特
This is my model
class ProductItemModel extends Equatable
{
final int id;
final String name;
final String price;
final String icon;
final bool isLove;
ProductItemModel({this.id, this.name, this.price, this.icon, this.isLove});
factory ProductItemModel.fromJson(Map<String, dynamic> item)
{
return ProductItemModel(
id: item["id"],
name: item["name"],
price: item["price"],
icon: item["icon"],
isLove: item["isLove"]
);
}
}
class ProductModel
{
List<ProductItemModel> listProduct;
ProductModel(this.listProduct);
factory ProductModel.fromJson(List<dynamic> json)
{
List<ProductItemModel> mapProduct = List<ProductItemModel>.from(json.map((item){
return ProductItemModel.fromJson(item);
}));
return ProductModel(mapProduct);
}
}