我想为商店应用程序构建UI,并且我不想为每个产品制作一个文件,所以我制作了一个名为Products的文件,我遇到的问题是我不太了解我如何传递图像数据。
class Products {
String id;
String title;
double price;
AssetImage image;
Products({
@required this.id,
@required this.title,
@required this.price,
@required this.image,
});
}
这是我在上面使用模型的列表。
final List<Products> _products = [
Products(
id: '1',
title: 'new shoes',
price: 69.99,
image: Image.asset('assets/images/BeatsPro'), //does not work...don't know how
),];
非常感谢您
答案 0 :(得分:2)
class Products {
String id;
String title;
double price;
String image;
Products({
@required this.id,
@required this.title,
@required this.price,
@required this.image,
});
}
final List<Products> _products = [
Products(
id: '1',
title: 'new shoes',
price: 69.99,
image: 'assets/images/BeatsPro.png',
),
];
显示多张图片
_products.map((product){
Image(image: AssetImage(product.image));
}).toList();
答案 1 :(得分:0)
因为
var a = Image.asset(''); // type Image
但是在“产品”中,您希望将AssetImage作为输入
class Products {
AssetImage image;
}
并且不能将'Image'类型的值分配给'AssetImage'类型的变量
答案 2 :(得分:0)
尝试这种方式
class Activity {
String imageUrl;
String name;
String type;
List<String> startTimes;
int rating;
int price;
Activity({
this.imageUrl,
this.name,
this.type,
this.startTimes,
this.rating,
this.price,
});
}
并创建另一个类
class Destination {
String imageUrl;
String city;
String country;
String description;
List<Activity> activities;
Destination({
this.imageUrl,
this.city,
this.country,
this.description,
this.activities,
});
}
List<Activity> activities = [
Activity(
imageUrl: 'assets/images/stmarksbasilica.jpg',
name: 'St. Mark\'s Basilica',
type: 'Sightseeing Tour',
startTimes: ['9:00 am', '11:00 am'],
rating: 5,
price: 30,
),
并且在您的活动中
final Destination destination;
在build
函数上方定义
Image(
fit: BoxFit.cover,
image: AssetImage(
widget.destination.imageUrl,
),
),