我在项目上添加了一个DropdownMenuItem,每次单击一个项目时,它都试图更改它的值。但是它不起作用。有人遇到这种方法并成功了吗?
List<DropdownMenuItem<int>> listSearch = [];
listSearch.add(DropdownMenuItem(
child:Center(child: Image.asset("assets/ring.png", height: 30.0,width: 30.0,)),
value: 0,
));
listSearch.add(DropdownMenuItem(
child:Center(child: Image.asset("assets/hoo.png", height: 30.0,width: 30.0,)),
value: 0,
));
listSearch.add(DropdownMenuItem(
child:Center(child: Image.asset("assets/lock.png", height: 30.0,width: 30.0,)),
value: 0,
));
Widget dropdownSearch(){
int search = 0;
String dropValue;
return DropdownButton(
items: listSearch,
//hint: listSearch[search],
value: dropValue,
onChanged: (value) {
print('Selected item : $value');
search = value;
switch(search){
case 0:
print('clicks ring $search');
break;
case 1:
print('clicks hoo $search');
break;
case 2:
print('click lock $search');
break;
default:
break;
}
} ,
);
答案 0 :(得分:1)
欢迎您!
StatefulWidget
,而不是StatelessWidget
。请仔细阅读https://flutter.io/tutorials/interactive/#creating-stateful-widget。dropValue
应该是类属性,这是您的状态。DropdownItem
都具有不同的value
集(例如1,2,3)。onChanged
处理程序中,您将使用setState(() => dropValue = value);
更新周围小部件的状态,而DropdownButton
将显示所选的正确项目。