我是新手,我想知道是否可以在一个类的一个参数内添加多个值?
例如,对于参数storePageItemPrice,storePageItemName和storePageImage,我希望能够在每个商店中添加多个价格,商品名称和多个商品图片。
我该如何实现?
示例
class Stores {
String storeName;
String storeImage;
String storeDeliveryTime;
String deliveryCharges;
String storePageImage;
String storePageItemName;
String storePageItemPrice;
Stores.list({this.storeName, this.storeDeliveryTime,
this.storeImage,this.deliveryCharges,this.storePageImage,
this.storePageItemName, this.storePageItemPrice});
}
List<Stores> storesList = [
Stores.list(
storeName: “STORE 1”,
storeImage: "assets/images/STORE1FRONT”,
storeDeliveryTime: "25 min",
deliveryCharges: "£3.90",
storePageImage: “BE ABLE TO ADD MORE THAN ONE IMAGE“ (and then build
widget.storePageImage),
storePageItemName: “BE ABLE TO ADD MORE THAN ONE ITEM“,(and then
build widget.storePageItemName),
storePageItemPrice: "BE ABLE TO ADD MORE THAN ONE PRICE”(and then
build widget.storePageItemPrice),
),
Stores.list(
storeName: “STORE 2”,
storeImage: "assets/images/STORE2FRONT",
storeDeliveryTime: "25 min",
deliveryCharges: "£2.90",
storePageImage: “BE ABLE TO ADD MORE THAN ONE IMAGE“ (and then
build widget.storePageImage),
storePageItemName: “BE ABLE TO ADD MORE THAN ONE ITEM“,(and then
build widget.storePageItemName),
storePageItemPrice: "BE ABLE TO ADD MORE THAN ONE PRICE”(and then
build widget.storePageItemPrice),
),
];
视觉示例;这是主页https://imgur.com/a/0YiMUHj
点击餐厅后,它会导航到餐厅的页面(https://imgur.com/a/KbsIepO),其中添加了多个项目(连接到数据库后可以根据需要添加和删除)
首页代码
ConstrainedBox(
constraints: BoxConstraints(maxHeight: 1000),
child: ListView.builder(
shrinkWrap: true,
itemCount: storesList.length,
itemBuilder: (BuildContext context, int index) {
return Column(
children: <Widget>[
GestureDetector(
onTap: (){Navigator.of(context).push(new
MaterialPageRoute(
builder: (context) => new StoresPage(
storeName: storesList[index].storeName,
storeDeliveryTime:
storesList[index].storeDeliveryTime,
deliveryCharges:
storesList[index].deliveryCharges,
storePageImage:
storesList[index].storePageImage,
storePageItemName:
storesList[index].storePageItemName,
storePageItemPrice:
storesList[index].storePageItemPrice,
)));},
child: Padding(
padding: const EdgeInsets.only(top:5.0),
child: Column(
children: <Widget>[
Container(
height: 205,
width: 380,
child: Image(image:
AssetImage(storesList[index].storeImage),
fit: BoxFit.fill,
),
),
Padding(
padding: const EdgeInsets.only(left:
15.0, right: 15.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(storesList[index].storeName),
Row(children: <Widget>[
Icon(Icons.update, size: 20.0),
Text(storesList[index].storeDeliveryTime),
],
),
],
),
),
],
),
),
),
],
);
},
),
),
导航到商店页面
class StoresPage extends StatefulWidget {
String storeName;
String storeDeliveryTime;
String deliveryCharges;
List<String> storePageImage;
List<String> storePageItemName;
List<String> storePageItemPrice;
StoresPage({
this.storeName, this.storeDeliveryTime, this.deliveryCharges,
this.storePageImage, this.storePageItemName, this.storePageItemPrice
});
List<Stores> storesList = [
newStore1,
newStore2,
];
此代码无效
SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 10.0, top: 7.0),
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
image: DecorationImage(fit: BoxFit.fill,image:
AssetImage(widget.storePageImage()))
),
),
),
],
),
],
),
),
我尝试在商店页面中使用此代码,但没有任何反应
ConstrainedBox(constraints: BoxConstraints(maxHeight: 1000),
child: ListView.builder(
shrinkWrap: true,
itemCount: storesList.length,
itemBuilder: (BuildContext context, int index){
return Column(
children: <Widget>[
GestureDetector(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 10.0, top: 7.0),
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
image: DecorationImage(fit:
BoxFit.fill,image:
AssetImage(storesList[index].storePageImage.toString()))
),
),
),
],
),
),
],
);
},
),
),
答案 0 :(得分:0)
class Stores {
String storeName;
String storeImage;
String storeDeliveryTime;
String deliveryCharges;
List<String> storePageImage;
List<String> storePageItemName;
List<double> storePageItemPrice;
Stores.list({this.storeName, this.storeDeliveryTime,
this.storeImage,this.deliveryCharges,this.storePageImage,
this.storePageItemName, this.storePageItemPrice});
}
Stores newStore1 = Stores.list(
storeName: "STORE 1",
storeImage: "assets/images/STORE1FRONT",
storeDeliveryTime: "25 min",
deliveryCharges: "£2.90",
storePageImage: ["image1.jpg", "image2.jpg"],
storePageItemName: ["Apple", "Mango"],
storePageItemPrice: [100, 200],
);
Stores newStore2 = Stores.list(
storeName: "STORE 2",
storeImage: "assets/images/STORE2FRONT",
storeDeliveryTime: "25 min",
deliveryCharges: "£2.90",
storePageImage: ["image1.jpg", "image2.jpg"],
storePageItemName: ["Apple", "Mango"],
storePageItemPrice: [100, 200],
);
List<Stores> storesList = [
newStore1,
newStore2,
]
由于我们需要渲染多张图像,因此我们需要将其所有父图像相乘。
其填充,容器,BoxDecoration,DecorationImage的定义必须多次渲染。
这是我的花朵图像的结果。
这是您最新代码版本的修正
return Scaffold(
appBar: AppBar(title: Text("Restauran Menus")),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
for (var storeImage in storePageImage) // loops storePageImage
Padding(
padding: EdgeInsets.only(left: 10.0, top: 7.0),
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage(storeImage),
),
),
),
),
],
),
],
),
),
);
查看此仓库Github