每当我从下拉列表中选择时,ListView.Builder
中都会有 DropDownButton
,它会给出更改值,但不会反射(reflect)在 UI 上。
List _warranty = ["NONE","1 YEAR", "2 YEAR"];
List<DropdownMenuItem<String>> getDropDownWarranty() {
List<DropdownMenuItem<String>> items = new List();
for (String str in _warranty) {
items.add(new DropdownMenuItem(
value: str,
child: new Text(
str,
style: TextStyle(color: Colors.white, fontFamily: 'semibold'),
)));
}
return items;
}
这是我的 ListView
项目小部件
Widget item(AsyncSnapshot<CartBean> snapshot, int index) {
String _current = "";
return Column(
children: <Widget>[
Container(
height: 40,
decoration: BoxDecoration(color: MyColors.cartback),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: SizeConfig.screenWidth / 1.7,
padding: EdgeInsets.all(SizeConfig.blockSizeHorizontal * 2),
decoration: BoxDecoration(
color: MyColors.colorLightPrimary,
),
child: snapshot.data.data[index].lcwInfo.length > 0
? Theme(
data: Theme.of(context).copyWith(
brightness: Brightness.dark,
canvasColor: MyColors.colorPrimary),
child: dropDown(snapshot.data.data[index].lcwInfo,
_current,index), // Your Dropdown Code Here,
)
: FlatButton(
onPressed: () {},
padding: EdgeInsets.all(0),
child: Text(
"EXTEND WARRANTY",
style: TextStyle(
color: Colors.grey[300],
fontFamily: "semibold"),
),
),
),
FlatButton.icon(
onPressed: () {
removeCartItemService(
snapshot.data.data[index].productId, index);
},
icon: Image.asset(
'assets/icons/cancel.png',
width: 28,
height: 28,
),
label: Text(''),
)
],
),
),
],
);
}
此方法在ListView
Item widget中调用
Widget dropDown(List<LcwInfo> list, String _current,int index) {
List<DropdownMenuItem<String>> _dropDownWarranty;
_dropDownWarranty = getDropDownWarranty();
_current = _dropDownWarranty[0].value;
if (list.length > 0) {
return DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: _dropDownWarranty[0].value,
items: _dropDownWarranty,
onChanged: (String onValue) {
debugPrint("CURRENT VALUE:----------${onValue}");
updateWarrantyService(index,onValue,list[0]);
setState(() {
_dropDownWarranty[0] = onValue as DropdownMenuItem<String>;
});
},
),
);
}
}
所有上述方法都在 build(BuildContext context)
中声明
最佳答案
我用selectedItemValue
创建了一个变量来存储DropdownButton
中的选定值,在我将默认值设置为None all item 之前。
class ListViewDemo extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return ListViewDemoState();
}
}
class ListViewDemoState extends State<ListViewDemo> {
List<String> selectedItemValue = List<String>();
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("ListView"),
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: 20,
itemExtent: 50.0,
itemBuilder: (BuildContext context, int index) {
for (int i = 0; i < 20; i++) {
selectedItemValue.add("NONE");
}
return DropdownButton(
value: selectedItemValue[index].toString(),
items: _dropDownItem(),
onChanged: (value) {
selectedItemValue[index] = value;
setState(() {});
},
hint: Text('Select Gender'),
);
}),
)
],
));
}
}
List<DropdownMenuItem<String>> _dropDownItem() {
List<String> ddl = ["NONE", "1 YEAR", "2 YEAR"];
return ddl
.map((value) => DropdownMenuItem(
value: value,
child: Text(value),
))
.toList();
}
关于android - 在 flutter 中下拉 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58095879/