Pon*_*bao 9 dart dropdown flutter
我正在从 JSON 获取项目并将它们显示在下拉列表中。当这个人从下拉列表中选择一个项目时,我得到了选择,但所选项目没有改变。
例如,我们在列表中有 (tokyo, paris, new york)。默认选择是东京。当这个人选择巴黎时,我明白了,但下拉列表中的选择没有改变。
这是我的代码:
new DropdownButton(
value: cities.elementAt(0),
hint: new Text("Ville"),
items: cities.map((String value) {
return new DropdownMenuItem(
value: value,
child: new Row(
children: <Widget>[
new Icon(
Icons.location_city,
color: Colors.deepOrange,
),
new Text(value)
],
),
);
}).toList(),
onChanged: (String value) {
getTravelCity(value);
},
),
Run Code Online (Sandbox Code Playgroud)
当人选择一个项目时,它仍然显示默认值。
mah*_*mnj 18
确保您没有在 Widget 中声明 selectedValue 下面的示例非常适合我。
这是dartpad上的工作代码来测试它
var currentSelectedValue;
const deviceTypes = ["Mac", "Windows", "Mobile"];
Widget typeFieldWidget() {
return Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: FormField<String>(
builder: (FormFieldState<String> state) {
return InputDecorator(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
hint: Text("Select Device"),
value: currentSelectedValue,
isDense: true,
onChanged: (newValue) {
setState(() {
currentSelectedValue = newValue;
});
print(currentSelectedValue);
},
items: deviceTypes.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
);
},
),
);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
This is the solution: "StatefulBuilder()"
Future<void> AgregarContacto() async {
String dropdownValue = 'Exento';
return showDialog<void>(
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
title: Text('Agregar cliente', textAlign: TextAlign.center,),
content:
SingleChildScrollView(
child: ListBody(
children: <Widget>[
Center(
child: Material(
type: MaterialType.transparency,
child: DropdownButton<String>(
items: <String>[
'Responsable inscripto',
'Monotributista',
'Exento',
'Consumidor final',
'No responsable'
]
.map<DropdownMenuItem<String>>((
String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
); //DropMenuItem
}).toList(),
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
print("new${newValue}");
}); //setState
},
//OnChange
isExpanded: false,
hint: Text('Responsable inscripto',
style: TextStyle(color: Colors.black),),
),
), //Material
), //Center
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
}
);
},
);
}
Run Code Online (Sandbox Code Playgroud)
小智 0
因为您需要用“setState(() {});”更新“value:”参数 方法和新值必须与传递给“onChanged:”的类型相同。我建议您这样做,因为您可以动态更改图标。
1)在文件顶部声明一个新类;
class city {
final String name;
final IconData icon;
const city({
this.name,
this.icon,
});
}
Run Code Online (Sandbox Code Playgroud)
2)将其添加到您的小部件的状态中:
class yourState extend<myWidget>{
List<city> cities = [
new city(name: "tokio", icon: Icons.location_city),
new city(name: "paris", icon: Icons.location_city),
new city(name: "new york", icon: Icons.location_city),
];
int index = 0;
Widget drop() {
return DropdownButton(
value: cities[index],
hint: new Text("Ville"),
items: cities.map((city value) {
return new DropdownMenuItem(
value: value,
child: new Row(
children: <Widget>[
new Icon(
value.icon,
color: Colors.deepOrange,
),
new Text(value.name)
],
),
);
}).toList(),
onChanged: (city value) {
setState(() {
index = cities.indexOf(value);
print(index);
});
//getTravelCity(value.name);
});
}
}
Run Code Online (Sandbox Code Playgroud)
现在在您想要 DropDown 的位置调用“drop()”。再见!!
| 归档时间: |
|
| 查看次数: |
29223 次 |
| 最近记录: |