List 在Flatter中单击按钮时,如何在空动态列表中添加动态项? 处境
我有一个动态空列表,类型为List 在Flatter中单击按钮时,如何在空动态列表中添加动态项? 处境,list,flutter,List,Flutter,我有一个动态空列表,类型为shoe,如下所示 class Shoe { String brand; int quantity; String image; Color productColor; int indexNumber; Shoe({String b, int q, String i, Color c, int iN}) { brand = b; quantity = q; image = i; productColor =
shoe
,如下所示
class Shoe {
String brand;
int quantity;
String image;
Color productColor;
int indexNumber;
Shoe({String b, int q, String i, Color c, int iN}) {
brand = b;
quantity = q;
image = i;
productColor = c;
indexNumber =iN;
}
}
动态空列表
List<Shoe> shoeCartList = [];
List shoeCartList=[];
当按下按钮时,如何在列表中动态添加项目
shoeCartList
。您可以使用shoeCartList
上的add
方法将项目[在您的情况下shoe objects
]添加到列表的末尾
我以您的代码为例添加了一个演示:
class ListExample extends StatelessWidget {
List<Shoe> shoeCartList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
height: 55,
width: double.infinity,
child: FlatButton(
onPressed: () {
// add items here [ use the .add method on the list]
shoeCartList.add(
Shoe(
b: VALUE OF b,
c: VALUE OF c,
i: VALUE OF i,
iN: VALUE OF iN,
q: VALUE OF q,
),
);
},
child: Center(
child: Text(
'Add Items',
),
),
),
),
),
);
}
}
class ListExample扩展了无状态小部件{
List shoeCartList=[];
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:大小盒子(
身高:55,
宽度:double.infinity,
孩子:扁平按钮(
已按下:(){
//在此处添加项目[使用列表上的.add方法]
shoeCartList.add(
鞋(
b:b的值,
c:c的值,
i:i的价值,
iN:iN的值,
q:q的值,
),
);
},
儿童:中心(
子:文本(
“添加项目”,
),
),
),
),
),
);
}
}
您可以使用shoeCartList
上的add
方法将项目[在您的情况下,鞋对象
]添加到列表的末尾
我以您的代码为例添加了一个演示:
class ListExample extends StatelessWidget {
List<Shoe> shoeCartList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
height: 55,
width: double.infinity,
child: FlatButton(
onPressed: () {
// add items here [ use the .add method on the list]
shoeCartList.add(
Shoe(
b: VALUE OF b,
c: VALUE OF c,
i: VALUE OF i,
iN: VALUE OF iN,
q: VALUE OF q,
),
);
},
child: Center(
child: Text(
'Add Items',
),
),
),
),
),
);
}
}
class ListExample扩展了无状态小部件{
List shoeCartList=[];
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:大小盒子(
身高:55,
宽度:double.infinity,
孩子:扁平按钮(
已按下:(){
//在此处添加项目[使用列表上的.add方法]
shoeCartList.add(
鞋(
b:b的值,
c:c的值,
i:i的价值,
iN:iN的值,
q:q的值,
),
);
},
儿童:中心(
子:文本(
“添加项目”,
),
),
),
),
),
);
}
}
List shoeCartList=[]
onPress(){
shoeCartList.add(Shoe(add your param));
}
List shoeCartList=[]
onPress(){
shoeCartList.add(Shoe(add your param));
}